/* bms.c
 * Author: Masanao Izumo <mo@goice.co.jp>
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>		/* for UCHAR_MAX */
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#ifdef __WIN32__
#include <windows.h>
#define MAXPATHLEN MAX_PATH
#define PATH_SEP '\\'
#define PATH_STR "\\"
#define IS_PATH_SEP(c) ((c) == '/' || (c) == '\\')
extern char *optarg;
extern int optind;
extern int getopt(int argc, char *argv[], char *optionS);
#define chdir(path) (SetCurrentDirectory(path) != 0 ? 0 : -1)
#else    
#include <sys/mman.h>
#include <sys/param.h>
#define PATH_SEP '/'
#define IS_PATH_SEP(c) ((c) == '/')
#define PATH_STR "/"
#endif /* __WIN32__ */
#include <sys/stat.h>
#include <errno.h>

#define BMS_VERSION "2.2.1"
#define VERBOSE 0
/* #define MEMCHR_FAST */

#if !defined(SVR4) && (defined(SYSTYPE_SVR4) || defined(__svr4__))
#define SVR4
#endif /* SVR4 */

#if defined(sun) && !defined(__svr4__) /* sun4 */
#include <memory.h>
#define memcpy(x, y, n) bcopy(y, x, n)
#endif /* sun */

#ifndef __WIN32__
#if !defined(sun) && defined(SVR4)
#include <sys/dir.h>
#define dirent direct
#else
#include <dirent.h>
#endif
#endif /* __WIN32__ */

#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif /* MAXPATHLEN */

#define FBAG_PACKSIZE 256
#define SIZEOF_FBAGNODE ((unsigned long)((struct FBagNode *)0)->bag)
#define HASHSIZE 1023

typedef struct FBagNode
{
    struct FBagNode* next;
    int  nelms;
    char bag[1];
} FBagNode;
typedef struct FBag
{
    FBagNode **entry;
    int size;
    int elmsiz;
} FBag;
void init_fbag(FBag* bag, int hash_size, int elmsiz);
void add_fbag(FBag* bag, void* elem);
int  search_fbag(FBag* bag, void* elem);
char* zcat(char* gzfile, long *size);

typedef struct _MAPFile
{
    void *map;
    long siz;
#ifdef __WIN32__
    HANDLE hFile, hMap;
#endif /* __WIN32__ */
} MAPFile;

int case_ignore = 0;
int jis_to_euc_flag = 0;
int around = 0;
int once_flag = 0;
int recursive_flag = 0;
int disp_lineno_flag = 0;
char* current_dir = NULL;
char subdir[MAXPATHLEN];
char* wild_pattern = NULL;
FBag bag_ino;
int disp_path_skip = 0;
int mark_flag;
char* match_in  = "\033[7m";
char* match_out = "\033[m";
int match_count_mode = 0;
int disp_except_match = 0;
int disp_filename = 0;
int only_disp_filename = 0;
int entirely_match = 0;
int zcat_flag = 0;
int directory_depth = 0;
#ifdef __WIN32__
int directory_max_depth = -1;
#else
int directory_max_depth = 15;
#endif /* __WIN32__ */
int cr2lf = 0;
int pettern_length;

struct file_id
{
    dev_t dev;
    ino_t ino;
};

/* ascii                ESC          $         [@B]
 *  ----+---->{0}----+----->{1}--+----->{2}--+-----> kanji-mode
 *     /|\           |           |           |
 *      |            |           |           |
 *      +------------+[^ESC]     |           |
 *      |                        |           |
 *      +------------------------+[^$]	     |
 *      |				     |
 *	+------------------------------------+[^@B]
 *
 * kanji                ESC          (         [JB]
 *  ----+---->{0}----+----->{1}--+----->{2}--+-----> ascii-mode
 *     /|\           |           |           |
 *      |            |           |           |
 *      +------------+[^ESC]     |           |
 *      |                        |           |
 *      +------------------------+[^(]	     |
 *      |			             |
 *	+------------------------------------+[^JB]
 */
#define ESC 033
long jistoeuc(unsigned char* eptr, unsigned char* jptr, long jlength)
{
    unsigned char* eptr_begin = eptr;
    long j;

/* Be careful: eptr - eptr_begin <= j */
    j = 0;
  ascii:
    while(j < jlength && jptr[j] != ESC)
    {
	int c = jptr[j++];
	if(c >= ' ' || c == '\n' || c == '\t') /* skip control sequences */
	    *eptr++ = c;
	else if(c == '\r' && jptr[j] != '\n') /* for *junet*mac */
	    *eptr++ = '\n';
    }
    if(j >= jlength)
	goto done;

    /* 1 */
    j++;
    if(jptr[j] != '$')		/* ?? \033[^$]  */
    {
	if(j == jlength)
	    goto done;
	goto ascii;
    }

    /* 2 */
    j++;
    if(jptr[j] != '@' && jptr[j] != 'B') /* ?? \033$[^@B] */
    {
	if(j == jlength)
	    goto done;
	goto ascii;
    }

  kanji:
    j++;
    /* -1 for truncated jis character */
    while(j < jlength - 1 && jptr[j] != ESC)
    {
	/* assume that correct jis code */
	*eptr++ = (jptr[j++] | 0x80);
	*eptr++ = (jptr[j++] | 0x80);
    }

    if(j == jlength - 1)
	*eptr++ = jptr[j++];
    if(j >= jlength)
	goto done;

    /* 1 */
    j++;
    if(jptr[j] != '(')
    {
	if(j == jlength)
	    goto done;
	goto kanji;
    }

    /* 2 */
    j++;
    if(jptr[j] != 'J' && jptr[j] != 'B')
    {
	if(j == jlength)
	    goto done;
	goto kanji;
    }

    j++;
    goto ascii;

  done:
    *eptr = '\0';		/* terminate a string */
    return (long)(eptr - eptr_begin);
}

static void convert_cr2lf(char *to, char *from, long n)
{
    long i;
    for(i = 0; i < n; i++)
    {
	if(from[i] == '\r')
	    to[i] = '\n';
	else
	    to[i] = from[i];
    }
}


const unsigned long crc_32_tab[256] = {
  0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  0x2d02ef8dL
};

static unsigned long hash(void* val, int size)
{
    unsigned long c = (unsigned long)0xffffffffL;
    unsigned char *p;
    int i;

    p = (unsigned char *)val;
    for(i = 0; i < size; i++)
	c = (c >> 8) ^ crc_32_tab[(c ^ p[i]) & 0xff];
    return c ^ 0xffffffffL;
}


void init_fbag(FBag* bag, int hash_size, int elmsiz)
{
    int i;

    if((bag->entry = (FBagNode **)malloc(hash_size * sizeof(FBagNode *)))
       == NULL)
    {
	perror("malloc");
	exit(1);
    }
    for(i = 0; i < hash_size; i++)
	bag->entry[i] = NULL;
    bag->size = hash_size;
    bag->elmsiz = elmsiz;
}

void add_fbag(FBag* bag, void* elem)
{
    int elmsiz = bag->elmsiz;
    unsigned int addr = hash(elem, elmsiz) % bag->size;
    FBagNode* np = bag->entry[addr];

    if(np == NULL)
    {
	if((np = (FBagNode *)malloc(SIZEOF_FBAGNODE + elmsiz * FBAG_PACKSIZE))
	   == NULL)
	{
	    perror("malloc");
	    exit(1);
	}
	np->nelms = 0;
	np->next = NULL;
	bag->entry[addr] = np;
    }
    else if(np->nelms == FBAG_PACKSIZE)
    {
	if((np = (FBagNode *)malloc(SIZEOF_FBAGNODE + elmsiz * FBAG_PACKSIZE))
	   == NULL)
	{
	    perror("malloc");
	    exit(1);
	}
	np->nelms = 0;
	np->next  = bag->entry[addr];
	bag->entry[addr] = np;
    }

    memcpy(np->bag + np->nelms * elmsiz, elem, elmsiz);
    np->nelms++;
}

int search_fbag(FBag* bag, void* elem)
{
    int elmsiz = bag->elmsiz;
    unsigned int addr = hash(elem, elmsiz) % bag->size;
    FBagNode* np = bag->entry[addr];
    int i, n;
    char* bp;

    while(np)
    {
	bp = np->bag;
	n  = np->nelms;
	for(i = 0; i < n; i++, bp += elmsiz)
	    if(memcmp(elem, bp, elmsiz) == 0)
		return 1;
	np = np->next;
    }
    return 0; /* Not found */
}

void make_bm_skip(const unsigned char* pattern, long* skip)
{
    long i, len;

    /* make the skip table */
    len = (long)strlen((char *)pattern);
    for(i = 0; i <= UCHAR_MAX; i++)
	skip[i] = len;
    for(i = 0; i < len - 1; i++)
	skip[pattern[i]] = len - 1 - i;
}

long BM_search(unsigned char* text, unsigned char* pattern,
	       long tlen, long* skip)
{
    long i, j, k, len;
    unsigned char c, tail;

    len = (long)strlen((char *)pattern);
    if(len == 0)
	return -1;

    if(len == tlen)
    {
	if(strcmp((const char *)text, (const char *)pattern) == 0)
	    return 0;
	return -1;
    }
    tail = pattern[len - 1];

    if(len == 1)
    {
	for(i = 0; i < tlen; i++)
	    if(text[i] == tail)
		return i;
    }
    else
    {
	/* matching procedure */

#ifndef MEMCHR_FAST
	i = len - 1;
	while(i < tlen)
	{
	    if((c = text[i]) == tail)
	    {
		j = len - 1;
		k = i;
		while(pattern[--j] == text[--k])
		    if(j == 0)
			return k; /* found */
	    }
	    i += skip[c];
	}
#else
	unsigned char *tailp;
	long orig_tlen;

	orig_tlen = tlen;
	text += len - 1;
	tlen -= len - 1;
	i = skip[tail];
	while(tlen > 0 &&
	      (tailp = (unsigned char *)memchr(text, tail, tlen)) != NULL)
	{
	    k = (long)(tailp - text);
	    text += (k + i);
	    tlen -= (k + i);
	    j = len - 1;
	    while(pattern[--j] == *(--tailp))
		if(j == 0)
		    return orig_tlen - tlen - i; /* found */
	}
#endif
    }
    return -1;
}

long BM_search_case(unsigned char* text, unsigned char* pattern,
		    long tlen, long* skip)
{
    long i, j, k, len;
    unsigned char c, tail;

    len = (long)strlen((char *)pattern);
    if(len == 0)
	return -1;

    if(len == tlen)
    {
	if(strcasecmp((const char *)text, (const char *)pattern) == 0)
	    return 0;
	return -1;
    }
    tail = pattern[len - 1];

    if(len == 1)
    {
	for(i = 0; i < tlen; i++)
	    if(tolower(text[i]) == tail)
		return i;
    }
    else
    {
	/* matching procedure */
	i = len - 1;
	while(i < tlen)
	{
	    c = tolower(text[i]);
	    if(c == tail)
	    {
		j = len - 1;
		k = i;
		while(--j, --k, pattern[j] == tolower(text[k]))
		    if(j == 0)
			return k; /* found */
	    }
	    i += skip[c];
	}
    }
    return -1;
}

static const char* make_path_name(const char* fname)
{
    static char buff[BUFSIZ];
    if(fname == NULL)
	return "(standard input)";
    if(subdir[0])
    {
	int len;

	len = strlen(subdir + disp_path_skip);
	strcpy(buff, subdir + disp_path_skip);
	buff[len] = PATH_SEP;
	strcpy(buff + len + 1, fname);
	return buff;
    }
    return fname;
}

static void report_near(const char* fname,
			const char* text, long tlen, long pos,
			int around, int lineno)
{
    int i, mark;
    long match_start = pos;
    long match_end = pos + pettern_length;

    if(only_disp_filename)
    {
	puts(make_path_name(fname));
	fflush(stdout);
	return;
    }

    if(match_end > tlen)
	match_end = tlen;

    if(around == 0)
    {
	if(disp_filename && fname != NULL)
	{
	    fputs(make_path_name(fname), stdout);
	    putchar(':');
	}
	if(disp_lineno_flag)
	    fprintf(stdout, "%d:", lineno);
	while(pos > 0 && text[pos] != '\n')
	    pos--;
	if(text[pos] == '\n')
	    pos++;

	if(mark_flag)
	{
	    mark = 0;
	    while(pos < tlen && text[pos] != '\n')
	    {
		if(pos == match_start)
		{
		    mark = 1;
		    fputs(match_in, stdout);
		}
		else if(pos == match_end)
		{
		    mark = 0;
		    fputs(match_out, stdout);
		}
		putchar(text[pos]);
		pos++;
	    }
	    if(mark)
		fputs(match_out, stdout);
	}
	else
	    while(pos < tlen && text[pos] != '\n')
	    {
		putchar(text[pos]);
		pos++;
	    }
	putchar('\n');
	fflush(stdout);
	return;
    }

    if(disp_filename)
    {
	fputs(make_path_name(fname), stdout);
	putchar(':');
    }
    else
	fputs("Found:", stdout);
    if(disp_lineno_flag)
	fprintf(stdout, "%d:", lineno);
    putchar('\n');

    i = 0;
    for(;;)
    {
	while(pos > 0 && text[pos] != '\n')
	    pos--;
	if(pos == 0 || i == around)
	    break;
	pos--;
	i++;
    }
    if(text[pos] == '\n')
	pos++;
    i = -around;
    mark = 0;
    for(;;)
    {
	fputs("    ", stdout);
	while(pos < tlen && text[pos] != '\n')
	{
	    if(mark_flag && pos == match_start)
	    {
		mark = 1;
		fputs(match_in, stdout);
	    }
	    else if(mark_flag && pos == match_end)
	    {
		mark = 0;
		fputs(match_out, stdout);
	    }
	    putchar(text[pos]);
	    pos++;
	}
	if(mark)
	{
	    fputs(match_out, stdout);
	    mark = 0;
	}
	putchar('\n');
	if(pos == tlen || i == around)
	    break;
	pos++;
	i++;
    }

    if(mark)
	fputs(match_out, stdout);
    fflush(stdout);
}

#ifdef __WIN32__
int readfile(const char* fname, MAPFile *mp)
{
    int fd;
    HANDLE hFile, hMap;

    mp->map = NULL;

    hFile = CreateFile(fname, GENERIC_READ, 0, NULL,
		       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile == INVALID_HANDLE_VALUE)
	return 0;

    if(mp->siz == -1)
    {
	mp->siz = GetFileSize(hFile, NULL);
	if(mp->siz == 0xffffffff)
	{
	    CloseHandle(hFile);
	    return 0;
	}
    }

    hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY,
			     0, 0, NULL);
    if(hMap == NULL)
    {
	CloseHandle(hFile);
	return NULL;
    }
    mp->map = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
    if(mp->map == NULL)
    {
	CloseHandle(hMap);
	CloseHandle(hFile);
	return NULL;
    }
    mp->hFile = hFile;
    mp->hMap = hMap;
    return 1;
}
void end_readfile(MAPFile *mp)
{
    UnmapViewOfFile(mp->map);
    CloseHandle(mp->hMap);
    CloseHandle(mp->hFile);
}
#else
int readfile(const char* fname, MAPFile *mp)
{
    int fd;

    mp->map = NULL;
    if((fd = open(fname, O_RDONLY)) < 0)
    {
#if 0
	perror(make_path_name(fname));
#endif
	return 0;
    }

    if(mp->siz == -1)
    {
	struct stat buf;

	if(fstat(fd, &buf) < 0)
	{
#if VERBOSE
	    perror(make_path_name(fname));
#endif
	    close(fd);
	    return 0;
	}

	if(!S_ISREG(buf.st_mode))
	{
#if VERBOSE
	    fprintf(stderr,
		    "%s: Not a regular file.\n", make_path_name(fname));
#endif
	    close(fd);
	    return 0;
	}
	mp->siz = (long)buf.st_size;
    }

    if((mp->map = mmap(0, mp->siz, PROT_READ,
		       MAP_SHARED, fd, 0)) == (caddr_t)-1)
    {
	if(mp->siz > 0)
	    perror("mmap");
	close(fd);
	return 0;
    }
    close(fd);
    return 1;
}
void end_readfile(MAPFile *mp)
{
    munmap(mp->map, mp->siz);
}
#endif /* __WIN32__ */


static int bms_text(const char* text, unsigned char* ptn, long* skip,
		    long n, const char* fname)
{
    long pos, offset;
    long found, lineno;

    offset = 0;
    found = 0;
    lineno = 1;

    if(!disp_except_match)
    {
	while(offset < n)
	{
	    if(case_ignore)
		pos = BM_search_case((unsigned char *)text + offset,
				     ptn,
				     n - offset, skip);
	    else
		pos = BM_search((unsigned char *)text + offset,
				ptn,
				n - offset, skip);
	    if(pos == -1)
		break;
	    if(entirely_match)
	    {
		long i;
		int bolp, eolp;

		i = offset + pos;
		bolp = (i == 0 || text[i - 1] == '\n');
		eolp = ((i + pettern_length == n) ||
			(i + pettern_length < n &&
			 text[i + pettern_length] == '\n') ||
			(i + pettern_length + 1 < n &&
			 text[i + pettern_length] == '\r' &&
			 text[i + pettern_length + 1] == '\n'));
		if(!bolp || !eolp)
		{
		    offset += pos;
		    while(offset < n && text[offset++] != '\n')
			;
		    continue;
		}
	    }

	    found++;
	    if(match_count_mode)
	    {
		if(once_flag)
		    break;
		offset += pos;
		while(offset < n && text[offset++] != '\n')
		    ;
		continue;
	    }
	    if(disp_lineno_flag)
	    {
		long i;
		for(i = offset; i < pos + offset; i++)
		    lineno += (text[i] == '\n');
	    }
	    offset += pos;
	    report_near(fname, text, n, offset, around, lineno);
	    if(once_flag)
		break;
	    while(offset < n && text[offset++] != '\n');
	    lineno++;
	}
    }
    else
    {
	long i, skip_line;

	while(offset < n)
	{
	    skip_line = 1;
	    if(case_ignore)
		pos = BM_search_case((unsigned char *)text + offset,
				     ptn,
				     n - offset, skip);
	    else
		pos = BM_search((unsigned char *)text + offset,
				ptn,
				n - offset, skip);
	    if(pos != -1 && entirely_match)
	    {
		int bolp, eolp;
		i = offset + pos;
		bolp = (i == 0 || text[i - 1] == '\n');
		eolp = ((i + pettern_length == n) ||
			(i + pettern_length < n &&
			 text[i + pettern_length] == '\n') ||
			(i + pettern_length + 1 < n &&
			 text[i + pettern_length] == '\r' &&
			 text[i + pettern_length + 1] == '\n'));
		if(!bolp || !eolp)
		{
		    while(offset + pos < n &&
			  text[offset + pos] != '\n')
			pos++;
		    if(offset + pos < n && text[offset + pos] == '\n')
			pos++;
		    skip_line = 0;
		}
	    }

	    if(pos == -1)
		pos = n - offset;

	    for(i = offset; i < pos + offset; i++)
	    {
		if(text[i] == '\n' || i == n - 1)
		{
		    /* found no match line */
		    found++;
		    if(!match_count_mode)
		    {
			long j;
			if(i == 0)
			    j = 0;
			else
			    j = i - 1;
			report_near(fname, text, n, j, around, lineno);
		    }

		    lineno++;
		    if(once_flag)
			goto loopout;
		}
	    }
	    offset += pos;
	    if(skip_line)
		while(offset < n && text[offset++] != '\n')
		    ;
	    lineno++;
	}
    }
  loopout:
    return found;
}

char* readtty(long* nread);
int wildmat(char *text, char *p);
static int bms_file(char* fname, long n, unsigned char* ptn, long* skip)
{
    char* text;
    char* gzfile;
    int found, mapflag;

    if(fname == NULL)
    {
	text = readtty(&n);

	if(zcat_flag)
	{
	    gzfile = zcat(text, &n);
	    if(gzfile != NULL)
	    {
		free(text);
		text = gzfile;
	    }
	}

	if(jis_to_euc_flag)
	    n = jistoeuc((unsigned char *)text, (unsigned char *)text, n);
	if(cr2lf)
	    convert_cr2lf(text, text, n);
	found = bms_text(text, ptn, skip, n, NULL);
	free(text);
	if(match_count_mode && (!only_disp_filename || found))
	    printf("%d\n", found);
    }
    else
    {
	char* p;
	MAPFile mp;

	if(wild_pattern != NULL && wildmat(fname, wild_pattern) != 1)
	    return 0;

	mp.siz = n;
	if(!readfile(fname, &mp))
	    return 0;
	p = (char *)mp.map;
	n = mp.siz;

	mapflag = 1;

	if(zcat_flag)
	{
	    long newsize;
	    newsize = n;
	    gzfile = zcat(p, &newsize);
	    if(gzfile != NULL)
	    {
		end_readfile(&mp);
		p = gzfile;
		n = newsize;
		mapflag = 0;
	    }
	}

	if(!jis_to_euc_flag)
	{
	    text = p;
	    if(cr2lf)
	    {
		if((text = malloc(n + 1)) == NULL)
		{
		    perror("malloc");
		    exit(1);
		}
		convert_cr2lf(text, p, n);
		text[n] = '\0';
		if(mapflag)
		    end_readfile(&mp);
		else
		    free(p);
		mapflag = 0;
	    }
	}
	else
	{
	    long newsize;

	    if((text = malloc(n + 1)) == NULL)
	    {
		perror("malloc");
		exit(1);
	    }
	    newsize = jistoeuc((unsigned char *)text, (unsigned char *)p, n);
	    if(cr2lf)
		convert_cr2lf(text, text, newsize);
	    if(mapflag)
		end_readfile(&mp);
	    else
		free(p);
	    n = newsize;
	    text[n] = '\0';
	    mapflag = 0;
	}

	found = bms_text(text, ptn, skip, n, fname);

	if(mapflag)
	    end_readfile(&mp);
	else
	    free(text);
	if(match_count_mode && (!only_disp_filename || found))
	    printf("%s:%d\n", make_path_name(fname), found);
    }
    fflush(stdout);
    return found;
}

#ifdef __WIN32__
typedef struct _W32DirEnt
{
    HANDLE		h;
    WIN32_FIND_DATA	f;
    int			cnt;
    char		*dir;
} W32DirEnt;
static void init_w32dirent(W32DirEnt *p, char *dir)
{
    p->h = NULL;
    p->cnt = 0;
    p->dir = dir;
}
static char *next_w32dirent(W32DirEnt *p)
{
    if(p->cnt++ == 0)
    {
	if((p->h = FindFirstFile(p->dir, &p->f)) == INVALID_HANDLE_VALUE)
	    return NULL;
	return p->f.cFileName;
    }
    if(FindNextFile(p->h, &p->f))
	return p->f.cFileName;
    return NULL;
}
static void end_w32dirent(W32DirEnt *p)
{
    FindClose(p->h);
}

static int bms_directory(unsigned char* ptn, long* skip)
{
    int found, subdir_len;
    W32DirEnt dir;
    char *fname;

    if(directory_max_depth >= 0 && directory_depth > directory_max_depth)
	return 0;

#if VERBOSE
    printf("%s" PATH_STR ":\n", make_path_name(""));
#endif

    subdir_len = strlen(subdir);

    init_w32dirent(&dir, "*");

    found = 0;
    while((fname = next_w32dirent(&dir)) != NULL)
    {
	if(!strcmp(fname, ".") || !strcmp(fname, ".."))
	    continue;
	if(dir.f.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
	{
	    if(chdir(fname) < 0)
		continue;

	    if(strlen(fname) + subdir_len + 2 >= MAXPATHLEN)
	    {
		fprintf(stderr, "%s/%s: Path name is too long.\n",
			subdir, fname);
		continue;
	    }

	    subdir[subdir_len] = PATH_SEP;
	    strcpy(subdir + subdir_len + 1, fname);

	    directory_depth++;
	    found += bms_directory(ptn, skip);
	    directory_depth--;

	    subdir[subdir_len] = '\0';

//	    if(chdir(subdir) < 0)
	    if(chdir("..") < 0)
	    {
		perror(subdir);
		continue;
	    }
	}
	else
	    found += bms_file(fname, dir.f.nFileSizeLow, ptn, skip);
    }
    end_w32dirent(&dir);

    return found;
}
#else
static int bms_directory(unsigned char* ptn, long* skip)
{
    int found, subdir_len;
    DIR *dirp;
    struct dirent *d;
    struct stat sbuf;

    if(directory_max_depth >= 0 && directory_depth > directory_max_depth)
	return 0;

#if VERBOSE
    printf("%s" PATH_STR ":\n", make_path_name(""));
#endif

    subdir_len = strlen(subdir);

    if((dirp = opendir(".")) == NULL)
    {
	perror("opendir");
	return 0;
    }

    found = 0;
    while((d = readdir(dirp)) != NULL)
    {
	char* fname = d->d_name;
	if(d->d_ino == 0 || !strcmp(fname, ".") || !strcmp(fname, ".."))
	    continue;

	if(stat(fname, &sbuf) < 0)
	{
#if VERBOSE
	    perror(make_path_name(fname));
#endif
	    continue;
	}

	if(S_ISDIR(sbuf.st_mode))
	{
	    struct file_id fid;

	    memset(&fid, 0, sizeof(fid));
	    fid.dev = sbuf.st_dev;
	    fid.ino = sbuf.st_ino;
	    if(search_fbag(&bag_ino, &fid))
		continue;
	    add_fbag(&bag_ino, &fid);

	    if(chdir(fname) < 0)
	    {
#if VERBOSE
		perror(fname);
#endif
		continue;
	    }

	    if(strlen(fname) + subdir_len + 2 >= MAXPATHLEN)
	    {
		fprintf(stderr, "%s/%s: Path name is too long.\n",
			subdir, fname);
		continue;
	    }

	    subdir[subdir_len] = PATH_SEP;
	    strcpy(subdir + subdir_len + 1, fname);

	    directory_depth++;
	    found += bms_directory(ptn, skip);
	    directory_depth--;

	    subdir[subdir_len] = '\0';

	    if(chdir(subdir) < 0)
	    {
		perror(subdir);
		continue;
	    }
	}
	else if(S_ISREG(sbuf.st_mode))
	    found += bms_file(fname, (long)sbuf.st_size, ptn, skip);
    }
    closedir(dirp);

    return found;
}
#endif /* __WIN32__ */

void init_mark(const char* term)
{
    if(!strcmp(term, "iris-ansi"))
    {
	match_in = "\033[1;7m";
	return;
    }
}

char* progname;
void usage(void)
{
    fprintf(stderr, "usage: %s [-1cEhilMmnRrVvxz] [-C <number>] [-f <file>] [-e] pattern [file ...]\n", progname);
}

int main(int argc, char** argv)
{
    unsigned char* ptn = NULL;
    int found;
    long i, skip[UCHAR_MAX + 1];
#ifdef sun
    extern char *optarg;
    extern int optind;
#endif /* sun */

#ifdef __WIN32__
    SetThreadPriority(GetCurrentThread(),
		      THREAD_PRIORITY_HIGHEST);
#endif /* __WIN32__ */

    if((progname = strrchr(argv[0], PATH_SEP)) == NULL)
	progname = argv[0];
    else
	progname++;

    if(argc < 2)
    {
	usage();
	return 1;
    }

    mark_flag = isatty(1);

    while ((i = getopt(argc, argv, "1cC:d:Ee:f:hilMmnRrVvxz")) > 0)
    {
	switch(i)
	{
	  case '1':
	    once_flag = 1;
	    break;
	  case 'c':
	    match_count_mode = 1;
	    break;
	  case 'C':
	    around = atoi(optarg);
	    if(around < 0)
	    {
		fprintf(stderr, "Invalid number: %d\n", around);
		return 1;
	    }
	    break;
	  case 'd':
	    directory_max_depth = atoi(optarg);
	    break;
	  case 'E':
	    jis_to_euc_flag = 1;
	    break;
	  case 'e':
	    ptn = (unsigned char *)optarg;
	    break;
	  case 'f':
	    wild_pattern = optarg;
	    break;
	  case 'h':
	    usage();
	    return 0;
	  case 'i':
	    case_ignore = 1;
	    break;
	  case 'l':
	    only_disp_filename = 1;
	    break;
	  case 'M':
	    mark_flag = 0;
	    break;
	  case 'm':
	    mark_flag = 1;
	    break;
	  case 'n':
	    disp_lineno_flag = 1;
	    break;
	  case 'R':
	    cr2lf = 1;
	    break;
	  case 'r':
	    recursive_flag = 1;
	    break;
	  case 'V':
	    printf("bms Version %s\n", BMS_VERSION);
	    return 0;
	  case 'v':
	    disp_except_match = 1;
	    break;
	  case 'x':
	    entirely_match = 1;
	    break;
	  case 'z':
	    zcat_flag = 1;
	    break;
	  case '?':
	    usage();
	    return 1;
	}
    }

    if(ptn == NULL && optind >= argc)
    {
	usage();
	return 1;
    }

    if(disp_except_match)
	mark_flag = 0;

//    if(match_count_mode)
//	only_disp_filename = 0;

    if(only_disp_filename && !match_count_mode)
	once_flag = 1;

    if(ptn == NULL)
	ptn = (unsigned char *)argv[optind++];

    if(jis_to_euc_flag)
	jistoeuc(ptn, ptn, (long)strlen((char *)ptn));
    pettern_length = strlen((char *)ptn);
    if(mark_flag)
    {
	char* term;
	term = getenv("TERM");
	if(term != NULL)
	    init_mark(term);
    }

    if(case_ignore)
	for(i = 0; ptn[i]; i++)
	    if(isupper(ptn[i]))
		ptn[i] = tolower(ptn[i]);
    make_bm_skip(ptn, skip);
    found = 0;

    if(optind == argc)
    {
	disp_filename = 0;
	if(bms_file(NULL, -1, ptn, skip) == 0)
	    return 1;
	return 0;
    }

    if(recursive_flag)
	init_fbag(&bag_ino, HASHSIZE, sizeof(struct file_id));

    if(recursive_flag || argc - optind > 1)
	disp_filename = 1;

    for(; optind < argc; optind++)
    {
	char* fname;

	fname = argv[optind];

	if(strlen(fname) + 1 >= MAXPATHLEN)
	{
	    fprintf(stderr, "%s: Path name is too long.\n", fname);
	    continue;
	}

	if(!strcmp(fname, "-"))
	{
	    found += bms_file(NULL, -1, ptn, skip);
	}
	else
	{
	    if(recursive_flag)
	    {

#ifdef __WIN32__
		if(GetFileAttributes(fname) == FILE_ATTRIBUTE_DIRECTORY)
		{
		    if(current_dir == NULL)
		    {
			if((current_dir = getcwd(NULL, BUFSIZ)) == NULL)
			{
			    perror("getcwd");
			    return 1;
			}
		    }
#else
		struct stat sbuf;
		if(stat(fname, &sbuf) < 0)
		    continue;
		if(S_ISDIR(sbuf.st_mode))
		{
		    struct file_id fid;

		    if(current_dir == NULL)
		    {
			if((current_dir = getcwd(NULL, BUFSIZ)) == NULL)
			{
			    perror("getcwd");
			    return 1;
			}
		    }

		    memset(&fid, 0, sizeof(fid));
		    fid.dev = sbuf.st_dev;
		    fid.ino = sbuf.st_ino;
		    if(search_fbag(&bag_ino, &fid))
			continue;
		    add_fbag(&bag_ino, &fid);
#endif /* __WIN32__ */

		    if(chdir(fname) < 0)
		    {
#if VERBOSE
			perror(fname);
#endif
			chdir(current_dir);
			continue;
		    }

		    if(IS_PATH_SEP(fname[0]))
		    {
			disp_path_skip = 0;
			strcpy(subdir, fname);
		    }
		    else
		    {
			disp_path_skip = strlen(current_dir) + 1;
			if(disp_path_skip + strlen(fname) + 2 > MAXPATHLEN)
			{
			    fprintf(stderr, "Pathname is too long.\n");
			    continue;
			}
			strcpy(subdir, current_dir);
			strcat(subdir, PATH_STR);
			strcat(subdir, fname);
		    }

		    directory_depth++;
		    found += bms_directory(ptn, skip);
		    directory_depth--;
		    subdir[0] = '\0';
		    chdir(current_dir);
		}
		else
		    found += bms_file(fname, -1, ptn, skip);
	    }
	    else
		found += bms_file(fname, -1, ptn, skip);
	}
    }
    if(found == 0)
	return 1;
    return 0;
}

#define UNIT_SIZE 1048576	/* 1M */
#define READ_UNIT (1024*64)	/* 64k */

struct list
{
    struct list* next;
    int   n;
    char buffer[1];
};

static struct list* first = NULL;
static struct list* last = NULL;

static struct list* newlist(void)
{
    struct list* node;

    if((node = (struct list *)malloc(sizeof(struct list) + UNIT_SIZE)) == NULL)
    {
	perror("malloc");
	exit(1);
    }

    node->n = 0;
    node->next = NULL;

    return node;
}

static void next_string(char* s, int n)
{
    struct list* p;

    if(first == NULL)
	last = first = newlist();
    p = last;
    if(p->n + n < UNIT_SIZE)
    {
	memcpy(p->buffer + p->n, s, n);
	p->n += n;
    }
    else
    {
	int n0;

	n0 = UNIT_SIZE - p->n;
	memcpy(p->buffer + p->n, s, n0);
	p->n = UNIT_SIZE;
	last = p->next = newlist();
	p = last;
	memcpy(p->buffer, s + n0, n - n0);
	p->n = n - n0;
    }
}

static char* concat_string(char* s, long* nread)
{
    struct list* p;
    struct list* cur;
    char* start = s;

    cur = first;
    *nread = 0;
    while(cur != NULL)
    {
	p = cur;
	cur = cur->next;
	memcpy(s, p->buffer, p->n);
	s += p->n;
	*nread += p->n;
	free(p);
    }

    last = first = NULL;
    return start;
}

char* readtty(long* nread)
{
    char buff[READ_UNIT];
    int fd = 0;
    long n, t;
    char* s;

    t = 0;
    while((n = read(fd, buff, READ_UNIT)) > 0)
    {
	t += n;
	next_string(buff, n);
    }

    if(n < 0)
    {
	perror("read");
	exit(1);
    }

    if(t == 0)
    {
	s = (char *)malloc(1);
	s[0] = '\0';
	*nread = 0;
	return s;
    }

    if((s = (char *)malloc(t)) == NULL)
    {
	fprintf(stderr, "Can't allocate memory\n");
	exit(1);
    }

    return concat_string(s, nread);
}


/************** wildmat ***************/
/*  $Revision: 1.9 $
**
**  Do shell-style pattern matching for ?, \, [], and * characters.
**  Might not be robust in face of malformed patterns; e.g., "foo[a-"
**  could cause a segmentation violation.  It is 8bit clean.
**
**  Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
**  Rich $alz is now <rsalz@osf.org>.
**  April, 1991:  Replaced mutually-recursive calls with in-line code
**  for the star character.
**
**  Special thanks to Lars Mathiesen <thorinn@diku.dk> for the ABORT code.
**  This can greatly speed up failing wildcard patterns.  For example:
**	pattern: -*-*-*-*-*-*-12-*-*-*-m-*-*-*
**	text 1:	 -adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1
**	text 2:	 -adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1
**  Text 1 matches with 51 calls, while text 2 fails with 54 calls.  Without
**  the ABORT code, it takes 22310 calls to fail.  Ugh.  The following
**  explanation is from Lars:
**  The precondition that must be fulfilled is that DoMatch will consume
**  at least one character in text.  This is true if *p is neither '*' nor
**  '\0'.)  The last return has ABORT instead of FALSE to avoid quadratic
**  behaviour in cases like pattern "*a*b*c*d" with text "abcxxxxx".  With
**  FALSE, each star-loop has to run to the end of the text; with ABORT
**  only the last one does.
**
**  Once the control of one instance of DoMatch enters the star-loop, that
**  instance will return either TRUE or ABORT, and any calling instance
**  will therefore return immediately after (without calling recursively
**  again).  In effect, only one star-loop is ever active.  It would be
**  possible to modify the code to maintain this context explicitly,
**  eliminating all recursive calls at the cost of some complication and
**  loss of clarity (and the ABORT stuff seems to be unclear enough by
**  itself).  I think it would be unwise to try to get this into a
**  released version unless you have a good test data base to try it out
**  on.
*/

#define TRUE			1
#define FALSE			0
#define ABORT			-1


    /* What character marks an inverted character class? */
#define NEGATE_CLASS		'^'
    /* Is "*" a common pattern? */
#define OPTIMIZE_JUST_STAR
    /* Do tar(1) matching rules, which ignore a trailing slash? */
#undef MATCH_TAR_PATTERN


/*
**  Match text and p, return TRUE, FALSE, or ABORT.
*/
static int DoMatch(char *text, char *p)
{
    register int	last;
    register int	matched;
    register int	reverse;

    for ( ; *p; text++, p++) {
	if (*text == '\0' && *p != '*')
	    return ABORT;
	switch (*p) {
	case '\\':
	    /* Literal match with following character. */
	    p++;
	    /* FALLTHROUGH */
	default:
	    if (*text != *p)
		return FALSE;
	    continue;
	case '?':
	    /* Match anything. */
	    continue;
	case '*':
	    while (*++p == '*')
		/* Consecutive stars act just like one. */
		continue;
	    if (*p == '\0')
		/* Trailing star matches everything. */
		return TRUE;
	    while (*text)
		if ((matched = DoMatch(text++, p)) != FALSE)
		    return matched;
	    return ABORT;
	case '[':
	    reverse = p[1] == NEGATE_CLASS ? TRUE : FALSE;
	    if (reverse)
		/* Inverted character class. */
		p++;
	    matched = FALSE;
	    if (p[1] == ']' || p[1] == '-')
		if (*++p == *text)
		    matched = TRUE;
	    for (last = *p; *++p && *p != ']'; last = *p)
		/* This next line requires a good C compiler. */
		if (*p == '-' && p[1] != ']'
		    ? *text <= *++p && *text >= last : *text == *p)
		    matched = TRUE;
	    if (matched == reverse)
		return FALSE;
	    continue;
	}
    }

#ifdef	MATCH_TAR_PATTERN
    if (IS_PATH_SEP(*text))
	return TRUE;
#endif	/* MATCH_TAR_ATTERN */
    return *text == '\0';
}


/*
**  User-level routine.  Returns TRUE or FALSE.
*/
int wildmat(char *text, char *p)
{
#ifdef	OPTIMIZE_JUST_STAR
    if (p[0] == '*' && p[1] == '\0')
	return TRUE;
#endif	/* OPTIMIZE_JUST_STAR */
    return DoMatch(text, p) == TRUE;
}



/*
 * zcat
 */
typedef struct _InflateHandler* InflateHandler;

InflateHandler open_inflate_handler(
	long (* read_func)(char* buf, long size, void* user_val),
	void* user_val);

long inflate(InflateHandler decoder,
	     char* decode_buff,
	     long decode_buff_size);

void close_inflate_handler(InflateHandler decoder);

#define GZIP_ASCIIFLAG		(1u<<0)
#define GZIP_MULTIPARTFLAG	(1u<<1)
#define GZIP_EXTRAFLAG		(1u<<2)
#define GZIP_FILEFLAG		(1u<<3)
#define GZIP_COMMFLAG		(1u<<4)
#define GZIP_ENCFLAG		(1u<<5)
struct zcat_read_arg
{
    char* ptr;
    long len;
};

long zcat_reader(char* buf, long size, void* user_val)
{
    struct zcat_read_arg* arg;
    long n;

    arg = (struct zcat_read_arg *)user_val;
    n = arg->len;
    if(n > size)
	n = size;
    memcpy(buf, arg->ptr, n);
    arg->ptr += n;
    arg->len -= n;
    return n;
}

char* zcat(char* gzfile, long *size)
{
    int method, flags;
    unsigned char* p = (unsigned char *)gzfile;
    unsigned char* q = p + *size;
    InflateHandler decoder;
    struct zcat_read_arg zcat_arg;
    char *buff;
    long newsize, n, buffsize;

    /* check & skip gzip header */
    if(*size == 0 || p[0] != 0x1f || p[1] != 0x8b)
	return NULL;

    p += 2;

    /* Method */
    if(p >= q) return NULL;
    method = *p++;
    if(method != 8)
    {
	/* Not suppoted */
	return NULL;
    }

    /* Flags */
    if(p >= q) return NULL;
    flags = *p++;
    if(flags & GZIP_ENCFLAG)
	return NULL;

    /* Time, extra flags, OS type */
    p += 6;

    if(flags & GZIP_MULTIPARTFLAG)
    {
	/* part number */
	p += 2;
    }

    if(flags & GZIP_EXTRAFLAG)
    {
	unsigned short len;

	/* extra field */

	if(p + 1 >= q) return NULL;
	len = *p++;
	len |= ((unsigned short)(*p++)) << 8;
	p += len;
    }

    if(flags & GZIP_FILEFLAG)
    {
	/* file name */
	int c;

	do
	{
	    if(p >= q) return NULL;
	    c = *p++;
	} while(c != '\0');
    }

    if(flags & GZIP_COMMFLAG)
    {
	/* file name */
	int c;

	do
	{
	    if(p >= q) return NULL;
	    c = *p++;
	} while(c != '\0');
    }
    if(p >= q) return NULL;

    zcat_arg.ptr = (char *)p;
    zcat_arg.len = q - p;
    buffsize = (long)(zcat_arg.len * 1.5 + 256);
    buff = (char *)malloc(buffsize);
    newsize = 0;
    decoder = open_inflate_handler(zcat_reader, &zcat_arg);
    while(newsize < buffsize &&
	  (n = inflate(decoder, buff + newsize, buffsize - newsize)))
	newsize += n;
    close_inflate_handler(decoder);
    *size = newsize;
    return buff;
}


/* Memory block for decreasing malloc
 *
 * +------+    +------+             +-------+
 * |BLOCK1|--->|BLOCK2|---> ... --->|BLOCK N|---> NULL
 * +------+    +------+             +-------+
 *
 *
 * BLOCK:
 * +-----------------------+
 * | memory 1              |
 * |                       |
 * +-----------------------+
 * | memory 2              |
 * +-----------------------+
 * | memory 3              |
 * |                       |
 * |                       |
 * +-----------------------+
 * | unused ...            |
 * +-----------------------+
 */

#define MIN_MBLOCK_SIZE 8192

typedef struct _MBlockNode
{
    size_t block_size;
    size_t offset;
    struct _MBlockNode *next;
#ifndef MBLOCK_NOPAD
    void *pad;
#endif /* MBLOCK_NOPAD */
    char buffer[1];
} MBlockNode;

typedef struct _MBlockList
{
    MBlockNode *first;
    size_t allocated;
} MBlockList;

extern void init_mblock(MBlockList *mblock);
extern void *new_segment(MBlockList *mblock, size_t nbytes);
extern void reuse_mblock(MBlockList *mblock);

static MBlockNode *free_mblock_list = NULL;
#define ADDRALIGN 8
/* #define DEBUG */

void init_mblock(MBlockList *mblock)
{
    mblock->first = NULL;
    mblock->allocated = 0;
}

static MBlockNode *new_mblock_node(size_t n)
{
    MBlockNode *p;

    if(n > MIN_MBLOCK_SIZE)
    {
	if((p = (MBlockNode *)malloc(n + sizeof(MBlockNode))) == NULL)
	    return NULL;
	p->block_size = n;
    }
    else if(free_mblock_list == NULL)
    {
	if((p = (MBlockNode *)malloc(sizeof(MBlockNode)
				     + MIN_MBLOCK_SIZE)) == NULL)
	    return NULL;
	p->block_size = MIN_MBLOCK_SIZE;
    }
    else
    {
	p = free_mblock_list;
	free_mblock_list = free_mblock_list->next;
    }

    p->offset = 0;
    p->next = NULL;

    return p;
}

static int enough_block_memory(MBlockList *mblock, size_t n)
{
    size_t newoffset;

    if(mblock->first == NULL)
	return 0;

    newoffset = mblock->first->offset + n;

    if(newoffset < mblock->first->offset) /* exceed representable in size_t */
	return 0;

    if(newoffset > mblock->first->block_size)
	return 0;

    return 1;
}

void *new_segment(MBlockList *mblock, size_t nbytes)
{
    MBlockNode *p;
    void *addr;

    /* round up to ADDRALIGN */
    nbytes = ((nbytes + ADDRALIGN - 1) & ~(ADDRALIGN - 1));
    if(!enough_block_memory(mblock, nbytes))
    {
	p = new_mblock_node(nbytes);
	p->next = mblock->first;
	mblock->first = p;
	mblock->allocated += p->block_size;
    }
    else
	p = mblock->first;

    addr = (void *)(p->buffer + p->offset);
    p->offset += nbytes;

#ifdef DEBUG
    if(((unsigned long)addr) & (ADDRALIGN-1))
    {
	fprintf(stderr, "Bad address: 0x%x\n", addr);
	exit(1);
    }
#endif /* DEBUG */

    return addr;
}

static void reuse_mblock1(MBlockNode *p)
{
    if(p->block_size > MIN_MBLOCK_SIZE)
	free(p);
    else /* p->block_size <= MIN_MBLOCK_SIZE */
    {
	p->next = free_mblock_list;
	free_mblock_list = p;
    }
}

void reuse_mblock(MBlockList *mblock)
{
    MBlockNode *p;

    if((p = mblock->first) == NULL)
	return;			/* There is nothing to collect memory */

    while(p)
    {
	MBlockNode *tmp;

	tmp = p;
	p = p->next;
	reuse_mblock1(tmp);
    }
    init_mblock(mblock);
}


/* inflate.c -- Not copyrighted 1992 by Mark Adler
   version c10p1, 10 January 1993 */

/* You can do whatever you like with this source file, though I would
   prefer that if you modify it and redistribute it that you include
   comments to that effect with your name and the date.	 Thank you.
   [The history has been moved to the file ChangeLog.]
 */

/*
   Inflate deflated (PKZIP's method 8 compressed) data.	 The compression
   method searches for as much of the current string of bytes (up to a
   length of 258) in the previous 32K bytes.  If it doesn't find any
   matches (of at least length 3), it codes the next byte.  Otherwise, it
   codes the length of the matched string and its distance backwards from
   the current position.  There is a single Huffman code that codes both
   single bytes (called "literals") and match lengths.	A second Huffman
   code codes the distance information, which follows a length code.  Each
   length or distance code actually represents a base value and a number
   of "extra" (sometimes zero) bits to get to add to the base value.  At
   the end of each deflated block is a special end-of-block (EOB) literal/
   length code.	 The decoding process is basically: get a literal/length
   code; if EOB then done; if a literal, emit the decoded byte; if a
   length then get the distance and emit the referred-to bytes from the
   sliding window of previously emitted data.

   There are (currently) three kinds of inflate blocks: stored, fixed, and
   dynamic.  The compressor outputs a chunk of data at a time and decides
   which method to use on a chunk-by-chunk basis.  A chunk might typically
   be 32K to 64K, uncompressed.	 If the chunk is uncompressible, then the
   "stored" method is used.  In this case, the bytes are simply stored as
   is, eight bits per byte, with none of the above coding.  The bytes are
   preceded by a count, since there is no longer an EOB code.

   If the data are compressible, then either the fixed or dynamic methods
   are used.  In the dynamic method, the compressed data are preceded by
   an encoding of the literal/length and distance Huffman codes that are
   to be used to decode this block.  The representation is itself Huffman
   coded, and so is preceded by a description of that code.  These code
   descriptions take up a little space, and so for small blocks, there is
   a predefined set of codes, called the fixed codes.  The fixed method is
   used if the block ends up smaller that way (usually for quite small
   chunks); otherwise the dynamic method is used.  In the latter case, the
   codes are customized to the probabilities in the current block and so
   can code it much better than the pre-determined fixed codes can.

   The Huffman codes themselves are decoded using a multi-level table
   lookup, in order to maximize the speed of decoding plus the speed of
   building the decoding tables.  See the comments below that precede the
   lbits and dbits tuning parameters.
 */


/*
   Notes beyond the 1.93a appnote.txt:

   1. Distance pointers never point before the beginning of the output
      stream.
   2. Distance pointers can point back across blocks, up to 32k away.
   3. There is an implied maximum of 7 bits for the bit length table and
      15 bits for the actual data.
   4. If only one code exists, then it is encoded using one bit.  (Zero
      would be more efficient, but perhaps a little confusing.)	 If two
      codes exist, they are coded using one bit each (0 and 1).
   5. There is no way of sending zero distance codes--a dummy must be
      sent if there are none.  (History: a pre 2.0 version of PKZIP would
      store blocks with no distance codes, but this was discovered to be
      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
      zero distance codes, which is sent as one code of zero bits in
      length.
   6. There are up to 286 literal/length codes.	 Code 256 represents the
      end-of-block.  Note however that the static length tree defines
      288 codes just to fill out the Huffman codes.  Codes 286 and 287
      cannot be used though, since there is no length base or extra bits
      defined for them.	 Similarily, there are up to 30 distance codes.
      However, static trees define 32 codes (all 5 bits) to fill out the
      Huffman codes, but the last two had better not show up in the data.
   7. Unzip can check dynamic Huffman blocks for complete code sets.
      The exception is that a single code would not be complete (see #4).
   8. The five bits following the block type is really the number of
      literal codes sent minus 257.
   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
      (1+6+6).	Therefore, to output three times the length, you output
      three codes (1+1+1), whereas to output four times the same length,
      you only need two codes (1+3).  Hmm.
  10. In the tree reconstruction algorithm, Code = Code + Increment
      only if BitLength(i) is not zero.	 (Pretty obvious.)
  11. Correction: 4 Bits: # of Bit Length codes - 4	(4 - 19)
  12. Note: length code 284 can represent 227-258, but length code 285
      really is 258.  The last length deserves its own, short code
      since it gets used a lot in very redundant files.	 The length
      258 is special since 258 - 3 (the min match length) is 255.
  13. The literal/length and distance code bit lengths are read as a
      single stream of lengths.	 It is possible (and advantageous) for
      a repeat code (16, 17, or 18) to go across the boundary between
      the two sets of lengths.
 */


typedef unsigned char  uch;
typedef unsigned short ush;
typedef unsigned long  ulg;

#define STORED_BLOCK 0
#define STATIC_TREES 1
#define DYN_TREES    2
#define WSIZE 32768
#define INBUF_EXTRA  64
#define OUTBUF_EXTRA 2048

#ifdef SMALL_MEM
#  define INBUFSIZ  8192    /* input buffer size */
#else
#  define INBUFSIZ  32768  /* input buffer size */
#endif

#ifdef SMALL_MEM
#  define OUTBUFSIZ   8192  /* output buffer size */
#else
#  define OUTBUFSIZ  16384  /* output buffer size */
#endif


/* Huffman code lookup table entry--this entry is four bytes for machines
   that have 16-bit pointers (e.g. PC's in the small or medium model).
   Valid extra bits are 0..13.	e == 15 is EOB (end of block), e == 16
   means that v is a literal, 16 < e < 32 means that v is a pointer to
   the next table, which codes e - 16 bits, and lastly e == 99 indicates
   an unused code.  If a code with e == 99 is looked up, this implies an
   error in the data. */
struct huft {
    uch e;		/* number of extra bits or operation */
    uch b;		/* number of bits in this code or subcode */
    union {
	ush n;		/* literal, length base, or distance base */
	struct huft *t;	/* pointer to next level of table */
    } v;
};

/* Save to local */
#define BITS_SAVE \
  ulg bit_buf = decoder->bit_buf; \
  ulg bit_len = decoder->bit_len;

/* Restore to decoder */
#define BITS_RESTORE \
  decoder->bit_buf = bit_buf; \
  decoder->bit_len = bit_len;

#define MASK_BITS(n) ((((ulg)1)<<(n))-1)
#define GET_BYTE()  (decoder->inptr < decoder->insize ? decoder->inbuf[decoder->inptr++] : fill_inbuf(decoder))
#define NEEDBITS(n) {while(bit_len<(n)){bit_buf|=((ulg)GET_BYTE())<<bit_len;bit_len+=8;}}
#define GETBITS(n)  (bit_buf & MASK_BITS(n))
#define DUMPBITS(n) {bit_buf>>=(n);bit_len-=(n);}

/* variables */
struct _InflateHandler
{
    void* user_val;
    long (*read_func)(char *buf, long size, void* user_val);

    uch slide[2L * WSIZE];
    uch inbuf[INBUFSIZ + INBUF_EXTRA];
    unsigned wp;	/* current position in slide */
    unsigned insize;	/* valid bytes in inbuf */
    unsigned inptr;	/* index of next byte to be processed in inbuf */
    struct huft *fixed_tl;	/* inflate static */
    struct huft *fixed_td;	/* inflate static */
    int fixed_bl, fixed_bd;	/* inflate static */
    ulg bit_buf;	/* bit buffer */
    ulg bit_len;	/* bits in bit buffer */
    int method;
    int eof;
    unsigned copy_leng;
    unsigned copy_dist;
    struct huft *tl, *td; /* literal/length and distance decoder tables */
    int bl, bd;		/* number of bits decoded by tl[] and td[] */
    MBlockList pool;	/* memory buffer for tl, td */
    unsigned char* obuf;
};

/* Function prototypes */
static int fill_inbuf(InflateHandler);
static int huft_build(unsigned *, unsigned, unsigned, ush *, ush *,
		     struct huft **, int *, MBlockList* pool);
static int huft_free(struct huft *);
static long inflate_codes(InflateHandler, char *, long);
static long inflate_stored(InflateHandler, char *, long);
static long inflate_fixed(InflateHandler, char *, long);
static long inflate_dynamic(InflateHandler, char *, long);
static void inflate_start(InflateHandler);

/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
   stream to find repeated byte strings.  This is implemented here as a
   circular buffer.  The index is updated simply by incrementing and then
   and'ing with 0x7fff (32K-1). */
/* It is left to other modules to supply the 32K area.	It is assumed
   to be usable as if it were declared "uch slide[32768];" or as just
   "uch *slide;" and then malloc'ed in the latter case.	 The definition
   must be in unzip.h, included above. */

#define lbits 9			/* bits in base literal/length lookup table */
#define dbits 6			/* bits in base distance lookup table */

/* Tables for deflate from PKZIP's appnote.txt. */
static ush cplens[] = {		/* Copy lengths for literal codes 257..285 */
	3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
	35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
	/* note: see note #13 above about the 258 in this list. */
static ush cplext[] = {		/* Extra bits for literal codes 257..285 */
	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
	3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
static ush cpdist[] = {		/* Copy offsets for distance codes 0..29 */
	1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
	257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
	8193, 12289, 16385, 24577};
static ush cpdext[] = {		/* Extra bits for distance codes */
	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
	7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
	12, 12, 13, 13};

/*
   Huffman code decoding is performed using a multi-level table lookup.
   The fastest way to decode is to simply build a lookup table whose
   size is determined by the longest code.  However, the time it takes
   to build this table can also be a factor if the data being decoded
   are not very long.  The most common codes are necessarily the
   shortest codes, so those codes dominate the decoding time, and hence
   the speed.  The idea is you can have a shorter table that decodes the
   shorter, more probable codes, and then point to subsidiary tables for
   the longer codes.  The time it costs to decode the longer codes is
   then traded against the time it takes to make longer tables.

   This results of this trade are in the variables lbits and dbits
   below.  lbits is the number of bits the first level table for literal/
   length codes can decode in one step, and dbits is the same thing for
   the distance codes.	Subsequent tables are also less than or equal to
   those sizes.	 These values may be adjusted either when all of the
   codes are shorter than that, in which case the longest code length in
   bits is used, or when the shortest code is *longer* than the requested
   table size, in which case the length of the shortest code in bits is
   used.

   There are two different values for the two tables, since they code a
   different number of possibilities each.  The literal/length table
   codes 286 possible values, or in a flat code, a little over eight
   bits.  The distance table codes 30 possible values, or a little less
   than five bits, flat.  The optimum values for speed end up being
   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
   The optimum values may differ though from machine to machine, and
   possibly even between compilers.  Your mileage may vary.
 */

/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
#define BMAX 16		/* maximum bit length of any code (16 for explode) */
#define N_MAX 288	/* maximum number of codes in any set */

static int huft_build(
    unsigned *b,	/* code lengths in bits (all assumed <= BMAX) */
    unsigned n,		/* number of codes (assumed <= N_MAX) */
    unsigned s,		/* number of simple-valued codes (0..s-1) */
    ush *d,		/* list of base values for non-simple codes */
    ush *e,		/* list of extra bits for non-simple codes */
    struct huft **t,	/* result: starting table */
    int *m,		/* maximum lookup bits, returns actual */
    MBlockList* pool)	/* memory pool */
/* Given a list of code lengths and a maximum table size, make a set of
   tables to decode that set of codes.	Return zero on success, one if
   the given code set is incomplete (the tables are still built in this
   case), two if the input is invalid (all zero length codes or an
   oversubscribed set of lengths), and three if not enough memory.
   The code with value 256 is special, and the tables are constructed
   so that no bits beyond that code are fetched when that code is
   decoded. */
{
    unsigned a;			/* counter for codes of length k */
    unsigned c[BMAX+1];		/* bit length count table */
    unsigned el;		/* length of EOB code (value 256) */
    unsigned f;			/* i repeats in table every f entries */
    int g;			/* maximum code length */
    int h;			/* table level */
    register unsigned i;	/* counter, current code */
    register unsigned j;	/* counter */
    register int k;		/* number of bits in current code */
    int lx[BMAX+1];		/* memory for l[-1..BMAX-1] */
    int *l = lx+1;		/* stack of bits per table */
    register unsigned *p;	/* pointer into c[], b[], or v[] */
    register struct huft *q;	/* points to current table */
    struct huft r;		/* table entry for structure assignment */
    struct huft *u[BMAX];	/* table stack */
    unsigned v[N_MAX];		/* values in order of bit length */
    register int w;		/* bits before this table == (l * h) */
    unsigned x[BMAX+1];		/* bit offsets, then code stack */
    unsigned *xp;		/* pointer into x */
    int y;			/* number of dummy codes added */
    unsigned z;			/* number of entries in current table */

    /* Generate counts for each bit length */
    el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
    memset(c, 0, sizeof(c));
    p = b;
    i = n;
    do
    {
	c[*p]++;	/* assume all entries <= BMAX */
	p++;		/* Can't combine with above line (Solaris bug) */
    } while(--i);
    if(c[0] == n)	/* null input--all zero length codes */
    {
	*t = (struct huft *)NULL;
	*m = 0;
	return 0;
    }

    /* Find minimum and maximum length, bound *m by those */
    for(j = 1; j <= BMAX; j++)
	if(c[j])
	    break;
    k = j;			/* minimum code length */
    if((unsigned)*m < j)
	*m = j;
    for(i = BMAX; i; i--)
	if(c[i])
	    break;
    g = i;			/* maximum code length */
    if((unsigned)*m > i)
	*m = i;

    /* Adjust last length count to fill out codes, if needed */
    for(y = 1 << j; j < i; j++, y <<= 1)
	if((y -= c[j]) < 0)
	    return 2;		/* bad input: more codes than bits */
    if((y -= c[i]) < 0)
	return 2;
    c[i] += y;

    /* Generate starting offsets into the value table for each length */
    x[1] = j = 0;
    p = c + 1;  xp = x + 2;
    while(--i)			/* note that i == g from above */
	*xp++ = (j += *p++);

    /* Make a table of values in order of bit lengths */
    memset(v, 0, sizeof(v));
    p = b;
    i = 0;
    do
    {
	if((j = *p++) != 0)
	    v[x[j]++] = i;
    } while(++i < n);
    n = x[g];			/* set n to length of v */

    /* Generate the Huffman codes and for each, make the table entries */
    x[0] = i = 0;		/* first Huffman code is zero */
    p = v;			/* grab values in bit order */
    h = -1;			/* no tables yet--level -1 */
    w = l[-1] = 0;		/* no bits decoded yet */
    u[0] = (struct huft *)NULL;	/* just to keep compilers happy */
    q = (struct huft *)NULL;	/* ditto */
    z = 0;			/* ditto */

    /* go through the bit lengths (k already is bits in shortest code) */
    for(; k <= g; k++)
    {
	a = c[k];
	while(a--)
	{
	    /* here i is the Huffman code of length k bits for value *p */
	    /* make tables up to required level */
	    while(k > w + l[h])
	    {
		w += l[h++];	/* add bits already decoded */

		/* compute minimum size table less than or equal to *m bits */
		z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */
		if((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
		{		/* too few codes for k-w bit table */
		    f -= a + 1;	/* deduct codes from patterns left */
		    xp = c + k;
		    while(++j < z)/* try smaller tables up to z bits */
		    {
			if((f <<= 1) <= *++xp)
			    break;	/* enough codes to use up j bits */
			f -= *xp;	/* else deduct codes from patterns */
		    }
		}
		if((unsigned)w + j > el && (unsigned)w < el)
		    j = el - w;	/* make EOB code end at table */
		z = 1 << j;	/* table entries for j-bit table */
		l[h] = j;	/* set table size in stack */

		/* allocate and link in new table */
		if(pool == NULL)
		    q = (struct huft *)malloc((z + 1)*sizeof(struct huft));
		else
		    q = (struct huft *)
			new_segment(pool, (z + 1)*sizeof(struct huft));
		if(q == NULL)
		{
		    if(h && pool == NULL)
			huft_free(u[0]);
		    return 3;	/* not enough memory */
		}

		*t = q + 1;	/* link to list for huft_free() */
		*(t = &(q->v.t)) = (struct huft *)NULL;
		u[h] = ++q;	/* table starts after link */

		/* connect to last table, if there is one */
		if(h)
		{
		    x[h] = i;		/* save pattern for backing up */
		    r.b = (uch)l[h-1];	/* bits to dump before this table */
		    r.e = (uch)(16 + j);/* bits in this table */
		    r.v.t = q;		/* pointer to this table */
		    j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
		    u[h-1][j] = r;	/* connect to last table */
		}
	    }

	    /* set up table entry in r */
	    r.b = (uch)(k - w);
	    if(p >= v + n)
		r.e = 99;		/* out of values--invalid code */
	    else if(*p < s)
	    {
		r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
		r.v.n = (ush)*p++;	/* simple code is just the value */
	    }
	    else
	    {
		r.e = (uch)e[*p - s];	/* non-simple--look up in lists */
		r.v.n = d[*p++ - s];
	    }

	    /* fill code-like entries with r */
	    f = 1 << (k - w);
	    for(j = i >> w; j < z; j += f)
		q[j] = r;

	    /* backwards increment the k-bit code i */
	    for(j = 1 << (k - 1); i & j; j >>= 1)
		i ^= j;
	    i ^= j;

	    /* backup over finished tables */
	    while((i & ((1 << w) - 1)) != x[h])
		w -= l[--h];		/* don't need to update q */
	}
    }

    /* return actual size of base table */
    *m = l[0];

    /* Return true (1) if we were given an incomplete table */
    return y != 0 && g != 1;
}

static int huft_free(struct huft *t)
/* Free the malloc'ed tables built by huft_build(), which makes a linked
   list of the tables it made, with the links in a dummy first entry of
   each table. */
{
    register struct huft *p, *q;

    /* Go through linked list, freeing from the malloced (t[-1]) address. */
    p = t;
    while(p != (struct huft *)NULL)
    {
	q = (--p)->v.t;
	free((char*)p);
	p = q;
    }
    return 0;
}

static long inflate_codes(InflateHandler decoder, char* buff, long size)
/* inflate (decompress) the codes in a deflated (compressed) block.
   Return an error code or zero if it all goes ok. */
{
    register unsigned e;/* table entry flag/number of extra bits */
    struct huft *t;	/* pointer to table entry */
    int n;
    struct huft *tl, *td;/* literal/length and distance decoder tables */
    int bl, bd;		/* number of bits decoded by tl[] and td[] */
    unsigned l, w, d;
    uch *slide;

    BITS_SAVE;

    if(size == 0)
	return 0;

    slide = decoder->slide;
    tl = decoder->tl;
    td = decoder->td;
    bl = decoder->bl;
    bd = decoder->bd;

#ifdef DEBUG
    if(decoder->copy_leng != 0)
    {
	fprintf(stderr, "What ? (decoder->copy_leng = %d)\n",
		decoder->copy_leng);
	abort();
    }
#endif /* DEBUG */
    w = decoder->wp;

    /* inflate the coded data */
    n = 0;
    for(;;)			/* do until end of block */
    {
	NEEDBITS((unsigned)bl);
	t = tl + GETBITS(bl);
	e = t->e;
	while(e > 16)
	{
	    if(e == 99)
		return -1;
	    DUMPBITS(t->b);
	    e -= 16;
	    NEEDBITS(e);
	    t = t->v.t + GETBITS(e);
	    e = t->e;
	}
	DUMPBITS(t->b);

	if(e == 16)		/* then it's a literal */
	{
	    w &= WSIZE - 1;
	    buff[n++] = slide[w++] = (uch)t->v.n;
	    if(n == size)
	    {
		decoder->wp = w;
		BITS_RESTORE;
		return size;
	    }
	    continue;
	}

	/* exit if end of block */
	if(e == 15)
	    break;

	/* it's an EOB or a length */

	/* get length of block to copy */
	NEEDBITS(e);
	l = t->v.n + GETBITS(e);
	DUMPBITS(e);

	/* decode distance of block to copy */
	NEEDBITS((unsigned)bd);
	t = td + GETBITS(bd);
	e = t->e;
	while(e > 16)
	{
	    if(e == 99)
		return -1;
	    DUMPBITS(t->b);
	    e -= 16;
	    NEEDBITS(e);
	    t = t->v.t + GETBITS(e);
	    e = t->e;
	}
	DUMPBITS(t->b);
	NEEDBITS(e);
	d = w - t->v.n - GETBITS(e);
	DUMPBITS(e);

	/* do the copy */
	while(l > 0 && n < size)
	{
	    l--;
	    d &= WSIZE - 1;
	    w &= WSIZE - 1;
	    buff[n++] = slide[w++] = slide[d++];
	}

	if(n == size)
	{
	    decoder->copy_leng = l;
	    decoder->wp = w;
	    decoder->copy_dist = d;
	    BITS_RESTORE;
	    return n;
	}
    }

    decoder->wp = w;
    decoder->method = -1; /* done */
    BITS_RESTORE;
    return n;
}

static long inflate_stored(InflateHandler decoder, char* buff, long size)
/* "decompress" an inflated type 0 (stored) block. */
{
    unsigned n, l, w;
    BITS_SAVE;

    /* go to byte boundary */
    n = bit_len & 7;
    DUMPBITS(n);

    /* get the length and its complement */
    NEEDBITS(16);
    n = GETBITS(16);
    DUMPBITS(16);
    NEEDBITS(16);
    if(n != (unsigned)((~bit_buf) & 0xffff))
    {
	BITS_RESTORE;
	return -1;			/* error in compressed data */
    }
    DUMPBITS(16);

    /* read and output the compressed data */
    decoder->copy_leng = n;

    n = 0;
    l = decoder->copy_leng;
    w = decoder->wp;
    while(l > 0 && n < size)
    {
	l--;
	w &= WSIZE - 1;
	NEEDBITS(8);
	buff[n++] = decoder->slide[w++] = (uch)GETBITS(8);
	DUMPBITS(8);
    }
    if(l == 0)
	decoder->method = -1; /* done */
    decoder->copy_leng = l;
    decoder->wp = w;
    BITS_RESTORE;
    return (long)n;
}

static long inflate_fixed(InflateHandler decoder, char* buff, long size)
/* decompress an inflated type 1 (fixed Huffman codes) block.  We should
   either replace this with a custom decoder, or at least precompute the
   Huffman tables. */
{
    /* if first time, set up tables for fixed blocks */
    if(decoder->fixed_tl == NULL)
    {
	int i;		  /* temporary variable */
	unsigned l[288];	  /* length list for huft_build */

	/* literal table */
	for(i = 0; i < 144; i++)
	    l[i] = 8;
	for(; i < 256; i++)
	    l[i] = 9;
	for(; i < 280; i++)
	    l[i] = 7;
	for(; i < 288; i++)	  /* make a complete, but wrong code set */
	    l[i] = 8;
	decoder->fixed_bl = 7;
	if((i = huft_build(l, 288, 257, cplens, cplext,
			   &decoder->fixed_tl, &decoder->fixed_bl, NULL))
	    != 0)
	{
	    decoder->fixed_tl = NULL;
	    return -1;
	}

	/* distance table */
	for(i = 0; i < 30; i++)	  /* make an incomplete code set */
	    l[i] = 5;
	decoder->fixed_bd = 5;
	if((i = huft_build(l, 30, 0, cpdist, cpdext,
			   &decoder->fixed_td, &decoder->fixed_bd, NULL)) > 1)
	{
	    huft_free(decoder->fixed_tl);
	    decoder->fixed_tl = NULL;
	    return -1;
	}
    }

    decoder->tl = decoder->fixed_tl;
    decoder->td = decoder->fixed_td;
    decoder->bl = decoder->fixed_bl;
    decoder->bd = decoder->fixed_bd;
    return inflate_codes(decoder, buff, size);
}

static long inflate_dynamic(InflateHandler decoder, char* buff, long size)
/* decompress an inflated type 2 (dynamic Huffman codes) block. */
{
    int i;		/* temporary variables */
    unsigned j;
    unsigned l;		/* last length */
    unsigned n;		/* number of lengths to get */
    struct huft *tl;	/* literal/length code table */
    struct huft *td;	/* distance code table */
    int bl;		/* lookup bits for tl */
    int bd;		/* lookup bits for td */
    unsigned nb;	/* number of bit length codes */
    unsigned nl;	/* number of literal/length codes */
    unsigned nd;	/* number of distance codes */
#ifdef PKZIP_BUG_WORKAROUND
    unsigned ll[288+32];/* literal/length and distance code lengths */
#else
    unsigned ll[286+30];/* literal/length and distance code lengths */
#endif
    static unsigned border[] = {  /* Order of the bit length code lengths */
	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
    BITS_SAVE;

    reuse_mblock(&decoder->pool);

    /* read in table lengths */
    NEEDBITS(5);
    nl = 257 + GETBITS(5);	/* number of literal/length codes */
    DUMPBITS(5);
    NEEDBITS(5);
    nd = 1 + GETBITS(5);	/* number of distance codes */
    DUMPBITS(5);
    NEEDBITS(4);
    nb = 4 + GETBITS(4);	/* number of bit length codes */
    DUMPBITS(4);
#ifdef PKZIP_BUG_WORKAROUND
    if(nl > 288 || nd > 32)
#else
    if(nl > 286 || nd > 30)
#endif
    {
	BITS_RESTORE;
	return -1;		/* bad lengths */
    }

    /* read in bit-length-code lengths */
    for(j = 0; j < nb; j++)
    {
	NEEDBITS(3);
	ll[border[j]] = GETBITS(3);
	DUMPBITS(3);
    }
    for(; j < 19; j++)
	ll[border[j]] = 0;

    /* build decoding table for trees--single level, 7 bit lookup */
    bl = 7;
    if((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl, &decoder->pool)) != 0)
    {
	reuse_mblock(&decoder->pool);
	BITS_RESTORE;
	return -1;		/* incomplete code set */
    }

    /* read in literal and distance code lengths */
    n = nl + nd;
    i = l = 0;
    while((unsigned)i < n)
    {
	NEEDBITS((unsigned)bl);
	j = (td = tl + (GETBITS(bl)))->b;
	DUMPBITS(j);
	j = td->v.n;
	if(j < 16)		/* length of code in bits (0..15) */
	    ll[i++] = l = j;	/* save last length in l */
	else if(j == 16)	/* repeat last length 3 to 6 times */
	{
	    NEEDBITS(2);
	    j = 3 + GETBITS(2);
	    DUMPBITS(2);
	    if((unsigned)i + j > n)
	    {
		BITS_RESTORE;
		return -1;
	    }
	    while(j--)
		ll[i++] = l;
	}
	else if(j == 17)	/* 3 to 10 zero length codes */
	{
	    NEEDBITS(3);
	    j = 3 + GETBITS(3);
	    DUMPBITS(3);
	    if((unsigned)i + j > n)
	    {
		BITS_RESTORE;
		return -1;
	    }
	    while(j--)
		ll[i++] = 0;
	    l = 0;
	}
	else			/* j == 18: 11 to 138 zero length codes */
	{
	    NEEDBITS(7);
	    j = 11 + GETBITS(7);
	    DUMPBITS(7);
	    if((unsigned)i + j > n)
	    {
		BITS_RESTORE;
		return -1;
	    }
	    while(j--)
		ll[i++] = 0;
	    l = 0;
	}
    }

    BITS_RESTORE;

    /* free decoding table for trees */
    reuse_mblock(&decoder->pool);

    /* build the decoding tables for literal/length and distance codes */
    bl = lbits;
    i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl, &decoder->pool);
    if(bl == 0)			      /* no literals or lengths */
    i = 1;
    if(i)
    {
	if(i == 1)
	    fprintf(stderr, " incomplete literal tree\n");
	reuse_mblock(&decoder->pool);
	return -1;		/* incomplete code set */
    }
    bd = dbits;
    i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd, &decoder->pool);
    if(bd == 0 && nl > 257)    /* lengths but no distances */
    {
	fprintf(stderr, " incomplete distance tree\n");
	reuse_mblock(&decoder->pool);
	return -1;
    }

    if(i == 1) {
#ifdef PKZIP_BUG_WORKAROUND
	i = 0;
#else
	fprintf(stderr, " incomplete distance tree\n");
#endif
    }
    if(i)
    {
	reuse_mblock(&decoder->pool);
	return -1;
    }

    /* decompress until an end-of-block code */
    decoder->tl = tl;
    decoder->td = td;
    decoder->bl = bl;
    decoder->bd = bd;

    i = inflate_codes(decoder, buff, size);

    if(i == -1) /* error */
    {
	reuse_mblock(&decoder->pool);
	return -1;
    }

    /* free the decoding tables, return */
    return i;
}

static void inflate_start(InflateHandler decoder)
/* initialize window, bit buffer */
{
    decoder->wp = 0;
    decoder->bit_buf = 0;
    decoder->bit_len = 0;
    decoder->insize = decoder->inptr = 0;
    decoder->fixed_td = decoder->fixed_tl = NULL;
    decoder->method = -1;
    decoder->eof = 0;
    decoder->copy_leng = decoder->copy_dist = 0;
    decoder->tl = NULL;

    init_mblock(&decoder->pool);
}

/*ARGSUSED*/
static long default_read_func(char* buf, long size, void* v)
{
    return (long)fread(buf, 1, size, stdin);
}

InflateHandler open_inflate_handler(
    long (* read_func)(char* buf, long size, void* user_val),
    void* user_val)
{
    InflateHandler decoder;

    decoder = (InflateHandler)
	malloc(sizeof(struct _InflateHandler));
    inflate_start(decoder);
    decoder->user_val = user_val;
    if(read_func == NULL)
	decoder->read_func = default_read_func;
    else
	decoder->read_func = read_func;
    return decoder;
}

void close_inflate_handler(InflateHandler decoder)
{
    if(decoder->fixed_tl != NULL)
    {
	huft_free(decoder->fixed_td);
	huft_free(decoder->fixed_tl);
	decoder->fixed_td = decoder->fixed_tl = NULL;
    }
    reuse_mblock(&decoder->pool);
    free(decoder);
}

/* decompress an inflated entry */
long inflate(
    InflateHandler decoder,
    char* buff,
    long size)
{
    long n, i;

    n = 0;
    while(n < size)
    {
	if(decoder->eof && decoder->method == -1)
	    return n;

	if(decoder->copy_leng > 0)
	{
	    unsigned l, w, d;

	    l = decoder->copy_leng;
	    w = decoder->wp;
	    if(decoder->method != STORED_BLOCK)
	    {
		/* STATIC_TREES or DYN_TREES */
		d = decoder->copy_dist;
		while(l > 0 && n < size)
		{
		    l--;
		    d &= WSIZE - 1;
		    w &= WSIZE - 1;
		    buff[n++] = decoder->slide[w++] = decoder->slide[d++];
		}
		decoder->copy_dist = d;
	    }
	    else
	    {
		BITS_SAVE;
		while(l > 0 && n < size)
		{
		    l--;
		    w &= WSIZE - 1;
		    NEEDBITS(8);
		    buff[n++] = decoder->slide[w++] = (uch)GETBITS(8);
		    DUMPBITS(8);
		}
		BITS_RESTORE;
		if(l == 0)
		    decoder->method = -1; /* done */
	    }
	    decoder->copy_leng = l;
	    decoder->wp = w;
	    if(n == size)
		return n;
	}

	if(decoder->method == -1)
	{
	    BITS_SAVE;
	    if(decoder->eof)
	    {
		BITS_RESTORE;
		break;
	    }
	    /* read in last block bit */
	    NEEDBITS(1);
	    if(GETBITS(1))
		decoder->eof = 1;
	    DUMPBITS(1);

	    /* read in block type */
	    NEEDBITS(2);
	    decoder->method = (int)GETBITS(2);
	    DUMPBITS(2);
	    decoder->tl = NULL;
	    decoder->copy_leng = 0;
	    BITS_RESTORE;
	}

	switch(decoder->method)
	{
	  case STORED_BLOCK:
	    i = inflate_stored(decoder, buff + n, size - n);
	    break;

	  case STATIC_TREES:
	    if(decoder->tl != NULL)
		i = inflate_codes(decoder, buff + n, size - n);
	    else
		i = inflate_fixed(decoder, buff + n, size - n);
	    break;

	  case DYN_TREES:
	    if(decoder->tl != NULL)
		i = inflate_codes(decoder, buff + n, size - n);
	    else
		i = inflate_dynamic(decoder, buff + n, size - n);
	    break;

	  default: /* error */
	    i = -1;
	    break;
	}

	if(i == -1)
	{
	    if(decoder->eof)
		return 0;
	    return -1; /* error */
	}
	n += i;
    }
    return n;
}

/* ===========================================================================
 * Fill the input buffer. This is called only when the buffer is empty.
 */
static int fill_inbuf(InflateHandler decoder)
{
    int len;

    /* Read as much as possible */
    decoder->insize = 0;
    errno = 0;
    do {
	len = decoder->read_func((char*)decoder->inbuf + decoder->insize,
				 (long)(INBUFSIZ - decoder->insize),
				 decoder->user_val);
	if(len == 0 || len == EOF) break;
	decoder->insize += len;
    } while(decoder->insize < INBUFSIZ);

    if(decoder->insize == 0)
	return EOF;
    decoder->inptr = 1;
    return decoder->inbuf[0];
}


#ifdef __WIN32__
int	optind	= 1;	/* index of which argument is next	*/
char   *optarg;		/* pointer to argument of current option */
int	opterr	= 1;	/* allow error message	*/

static	char   *letP = NULL;	/* remember next option char's location */

#if 0
static	char	SW = 0;		/* DOS switch character, either '-' or '/' */
#endif
#define SW '-' /* On Win32 can't call DOS! */
/*
  Parse the command line options, System V style.

  Standard option syntax is:

    option ::= SW [optLetter]* [argLetter space* argument]

  where
    - SW is either '/' or '-', according to the current setting
      of the MSDOS switchar (int 21h function 37h).
    - there is no space before any optLetter or argLetter.
    - opt/arg letters are alphabetic, not punctuation characters.
	 - optLetters, if present, must be matched in optionS.
    - argLetters, if present, are found in optionS followed by ':'.
    - argument is any white-space delimited string.  Note that it
      can include the SW character.
    - upper and lower case letters are distinct.

  There may be multiple option clusters on a command line, each
  beginning with a SW, but all must appear before any non-option
  arguments (arguments not introduced by SW).  Opt/arg letters may
  be repeated: it is up to the caller to decide if that is an error.

  The character SW appearing alone as the last argument is an error.
  The lead-in sequence SWSW ("--" or "//") causes itself and all the
  rest of the line to be ignored (allowing non-options which begin
  with the switch char).

  The string *optionS allows valid opt/arg letters to be recognized.
  argLetters are followed with ':'.  Getopt () returns the value of
  the option character found, or EOF if no more options are in the
  command line.	 If option is an argLetter then the global optarg is
  set to point to the argument string (having skipped any white-space).

  The global optind is initially 1 and is always left as the index
  of the next argument of argv[] which getopt has not taken.  Note
  that if "--" or "//" are used then optind is stepped to the next
  argument before getopt() returns EOF.

  If an error occurs, that is an SW char precedes an unknown letter,
  then getopt() will return a '?' character and normally prints an
  error message via perror().  If the global variable opterr is set
  to false (zero) before calling getopt() then the error message is
  not printed.

  For example, if the MSDOS switch char is '/' (the MSDOS norm) and

    *optionS == "A:F:PuU:wXZ:"

  then 'P', 'u', 'w', and 'X' are option letters and 'F', 'U', 'Z'
  are followed by arguments.  A valid command line may be:

    aCommand  /uPFPi /X /A L someFile

  where:
    - 'u' and 'P' will be returned as isolated option letters.
    - 'F' will return with "Pi" as its argument string.
    - 'X' is an isolated option.
    - 'A' will return with "L" as its argument.
    - "someFile" is not an option, and terminates getOpt.  The
      caller may collect remaining arguments using argv pointers.
*/

int	getopt(int argc, char *argv[], char *optionS)
{
	unsigned char ch;
	char *optP;

#if 0
	if (SW == 0) {
		/* get SW using dos call 0x37 */
		_AX = 0x3700;
		geninterrupt(0x21);
		SW = _DL;
	}
#endif
	if (argc > optind) {
		if (letP == NULL) {
			if ((letP = argv[optind]) == NULL ||
				*(letP++) != SW)  goto gopEOF;
			if (*letP == SW) {
				optind++;  goto gopEOF;
			}
		}
		if (0 == (ch = *(letP++))) {
			optind++;  goto gopEOF;
		}
		if (':' == ch  ||  (optP = strchr(optionS, ch)) == NULL)
			goto gopError;
		if (':' == *(++optP)) {
			optind++;
			if (0 == *letP) {
				if (argc <= optind)  goto  gopError;
				letP = argv[optind++];
			}
			optarg = letP;
			letP = NULL;
		} else {
			if (0 == *letP) {
				optind++;
				letP = NULL;
			}
			optarg = NULL;
		}
		return ch;
	}
gopEOF:
	optarg = letP = NULL;
	return EOF;

gopError:
        if (argc > optind)
                optind++;
        optarg = letP = NULL;
	errno  = EINVAL;
	if (opterr)
		perror ("get command line option");
	return ('?');
}
#endif /* __WIN32__ */
