Blame libtiff/tif_lzw.c

Packit 994f1a
/* $Id: tif_lzw.c,v 1.29.2.6 2010-06-08 18:50:42 bfriesen Exp $ */
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Copyright (c) 1988-1997 Sam Leffler
Packit 994f1a
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 994f1a
 *
Packit 994f1a
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit 994f1a
 * its documentation for any purpose is hereby granted without fee, provided
Packit 994f1a
 * that (i) the above copyright notices and this permission notice appear in
Packit 994f1a
 * all copies of the software and related documentation, and (ii) the names of
Packit 994f1a
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 994f1a
 * publicity relating to the software without the specific, prior written
Packit 994f1a
 * permission of Sam Leffler and Silicon Graphics.
Packit 994f1a
 * 
Packit 994f1a
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Packit 994f1a
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Packit 994f1a
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Packit 994f1a
 * 
Packit 994f1a
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 994f1a
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 994f1a
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 994f1a
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Packit 994f1a
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Packit 994f1a
 * OF THIS SOFTWARE.
Packit 994f1a
 */
Packit 994f1a
Packit 994f1a
#include "tiffiop.h"
Packit 994f1a
#ifdef LZW_SUPPORT
Packit 994f1a
/*
Packit 994f1a
 * TIFF Library.
Packit 994f1a
 * Rev 5.0 Lempel-Ziv & Welch Compression Support
Packit 994f1a
 *
Packit 994f1a
 * This code is derived from the compress program whose code is
Packit 994f1a
 * derived from software contributed to Berkeley by James A. Woods,
Packit 994f1a
 * derived from original work by Spencer Thomas and Joseph Orost.
Packit 994f1a
 *
Packit 994f1a
 * The original Berkeley copyright notice appears below in its entirety.
Packit 994f1a
 */
Packit 994f1a
#include "tif_predict.h"
Packit 994f1a
Packit 994f1a
#include <stdio.h>
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * NB: The 5.0 spec describes a different algorithm than Aldus
Packit 994f1a
 *     implements.  Specifically, Aldus does code length transitions
Packit 994f1a
 *     one code earlier than should be done (for real LZW).
Packit 994f1a
 *     Earlier versions of this library implemented the correct
Packit 994f1a
 *     LZW algorithm, but emitted codes in a bit order opposite
Packit 994f1a
 *     to the TIFF spec.  Thus, to maintain compatibility w/ Aldus
Packit 994f1a
 *     we interpret MSB-LSB ordered codes to be images written w/
Packit 994f1a
 *     old versions of this library, but otherwise adhere to the
Packit 994f1a
 *     Aldus "off by one" algorithm.
Packit 994f1a
 *
Packit 994f1a
 * Future revisions to the TIFF spec are expected to "clarify this issue".
Packit 994f1a
 */
Packit 994f1a
#define	LZW_COMPAT		/* include backwards compatibility code */
Packit 994f1a
/*
Packit 994f1a
 * Each strip of data is supposed to be terminated by a CODE_EOI.
Packit 994f1a
 * If the following #define is included, the decoder will also
Packit 994f1a
 * check for end-of-strip w/o seeing this code.  This makes the
Packit 994f1a
 * library more robust, but also slower.
Packit 994f1a
 */
Packit 994f1a
#define	LZW_CHECKEOS		/* include checks for strips w/o EOI code */
Packit 994f1a
Packit 994f1a
#define MAXCODE(n)	((1L<<(n))-1)
Packit 994f1a
/*
Packit 994f1a
 * The TIFF spec specifies that encoded bit
Packit 994f1a
 * strings range from 9 to 12 bits.
Packit 994f1a
 */
Packit 994f1a
#define	BITS_MIN	9		/* start with 9 bits */
Packit 994f1a
#define	BITS_MAX	12		/* max of 12 bit strings */
Packit 994f1a
/* predefined codes */
Packit 994f1a
#define	CODE_CLEAR	256		/* code to clear string table */
Packit 994f1a
#define	CODE_EOI	257		/* end-of-information code */
Packit 994f1a
#define CODE_FIRST	258		/* first free code entry */
Packit 994f1a
#define	CODE_MAX	MAXCODE(BITS_MAX)
Packit 994f1a
#define	HSIZE		9001L		/* 91% occupancy */
Packit 994f1a
#define	HSHIFT		(13-8)
Packit 994f1a
#ifdef LZW_COMPAT
Packit 994f1a
/* NB: +1024 is for compatibility with old files */
Packit 994f1a
#define	CSIZE		(MAXCODE(BITS_MAX)+1024L)
Packit 994f1a
#else
Packit 994f1a
#define	CSIZE		(MAXCODE(BITS_MAX)+1L)
Packit 994f1a
#endif
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * State block for each open TIFF file using LZW
Packit 994f1a
 * compression/decompression.  Note that the predictor
Packit 994f1a
 * state block must be first in this data structure.
Packit 994f1a
 */
Packit 994f1a
typedef	struct {
Packit 994f1a
	TIFFPredictorState predict;	/* predictor super class */
Packit 994f1a
Packit 994f1a
	unsigned short	nbits;		/* # of bits/code */
Packit 994f1a
	unsigned short	maxcode;	/* maximum code for lzw_nbits */
Packit 994f1a
	unsigned short	free_ent;	/* next free entry in hash table */
Packit 994f1a
	long		nextdata;	/* next bits of i/o */
Packit 994f1a
	long		nextbits;	/* # of valid bits in lzw_nextdata */
Packit 994f1a
Packit 994f1a
        int             rw_mode;        /* preserve rw_mode from init */
Packit 994f1a
} LZWBaseState;
Packit 994f1a
Packit 994f1a
#define	lzw_nbits	base.nbits
Packit 994f1a
#define	lzw_maxcode	base.maxcode
Packit 994f1a
#define	lzw_free_ent	base.free_ent
Packit 994f1a
#define	lzw_nextdata	base.nextdata
Packit 994f1a
#define	lzw_nextbits	base.nextbits
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Encoding-specific state.
Packit 994f1a
 */
Packit 994f1a
typedef uint16 hcode_t;			/* codes fit in 16 bits */
Packit 994f1a
typedef struct {
Packit 994f1a
	long	hash;
Packit 994f1a
	hcode_t	code;
Packit 994f1a
} hash_t;
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Decoding-specific state.
Packit 994f1a
 */
Packit 994f1a
typedef struct code_ent {
Packit 994f1a
	struct code_ent *next;
Packit 994f1a
	unsigned short	length;		/* string len, including this token */
Packit 994f1a
	unsigned char	value;		/* data value */
Packit 994f1a
	unsigned char	firstchar;	/* first token of string */
Packit 994f1a
} code_t;
Packit 994f1a
Packit 994f1a
typedef	int (*decodeFunc)(TIFF*, tidata_t, tsize_t, tsample_t);
Packit 994f1a
Packit 994f1a
typedef struct {
Packit 994f1a
	LZWBaseState base;
Packit 994f1a
Packit 994f1a
	/* Decoding specific data */
Packit 994f1a
	long	dec_nbitsmask;		/* lzw_nbits 1 bits, right adjusted */
Packit 994f1a
	long	dec_restart;		/* restart count */
Packit 994f1a
#ifdef LZW_CHECKEOS
Packit 994f1a
	long	dec_bitsleft;		/* available bits in raw data */
Packit 994f1a
#endif
Packit 994f1a
	decodeFunc dec_decode;		/* regular or backwards compatible */
Packit 994f1a
	code_t*	dec_codep;		/* current recognized code */
Packit 994f1a
	code_t*	dec_oldcodep;		/* previously recognized code */
Packit 994f1a
	code_t*	dec_free_entp;		/* next free entry */
Packit 994f1a
	code_t*	dec_maxcodep;		/* max available entry */
Packit 994f1a
	code_t*	dec_codetab;		/* kept separate for small machines */
Packit 994f1a
Packit 994f1a
	/* Encoding specific data */
Packit 994f1a
	int	enc_oldcode;		/* last code encountered */
Packit 994f1a
	long	enc_checkpoint;		/* point at which to clear table */
Packit 994f1a
#define CHECK_GAP	10000		/* enc_ratio check interval */
Packit 994f1a
	long	enc_ratio;		/* current compression ratio */
Packit 994f1a
	long	enc_incount;		/* (input) data bytes encoded */
Packit 994f1a
	long	enc_outcount;		/* encoded (output) bytes */
Packit 994f1a
	tidata_t enc_rawlimit;		/* bound on tif_rawdata buffer */
Packit 994f1a
	hash_t*	enc_hashtab;		/* kept separate for small machines */
Packit 994f1a
} LZWCodecState;
Packit 994f1a
Packit 994f1a
#define	LZWState(tif)		((LZWBaseState*) (tif)->tif_data)
Packit 994f1a
#define	DecoderState(tif)	((LZWCodecState*) LZWState(tif))
Packit 994f1a
#define	EncoderState(tif)	((LZWCodecState*) LZWState(tif))
Packit 994f1a
Packit 994f1a
static	int LZWDecode(TIFF*, tidata_t, tsize_t, tsample_t);
Packit 994f1a
#ifdef LZW_COMPAT
Packit 994f1a
static	int LZWDecodeCompat(TIFF*, tidata_t, tsize_t, tsample_t);
Packit 994f1a
#endif
Packit 994f1a
static  void cl_hash(LZWCodecState*);
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * LZW Decoder.
Packit 994f1a
 */
Packit 994f1a
Packit 994f1a
#ifdef LZW_CHECKEOS
Packit 994f1a
/*
Packit 994f1a
 * This check shouldn't be necessary because each
Packit 994f1a
 * strip is suppose to be terminated with CODE_EOI.
Packit 994f1a
 */
Packit 994f1a
#define	NextCode(_tif, _sp, _bp, _code, _get) {				\
Packit 994f1a
	if ((_sp)->dec_bitsleft < nbits) {				\
Packit 994f1a
		TIFFWarningExt(_tif->tif_clientdata, _tif->tif_name,				\
Packit 994f1a
		    "LZWDecode: Strip %d not terminated with EOI code", \
Packit 994f1a
		    _tif->tif_curstrip);				\
Packit 994f1a
		_code = CODE_EOI;					\
Packit 994f1a
	} else {							\
Packit 994f1a
		_get(_sp,_bp,_code);					\
Packit 994f1a
		(_sp)->dec_bitsleft -= nbits;				\
Packit 994f1a
	}								\
Packit 994f1a
}
Packit 994f1a
#else
Packit 994f1a
#define	NextCode(tif, sp, bp, code, get) get(sp, bp, code)
Packit 994f1a
#endif
Packit 994f1a
Packit 994f1a
static int
Packit 994f1a
LZWSetupDecode(TIFF* tif)
Packit 994f1a
{
Packit 994f1a
	LZWCodecState* sp = DecoderState(tif);
Packit 994f1a
	static const char module[] = " LZWSetupDecode";
Packit 994f1a
	int code;
Packit 994f1a
Packit 994f1a
        if( sp == NULL )
Packit 994f1a
        {
Packit 994f1a
            /*
Packit 994f1a
             * Allocate state block so tag methods have storage to record 
Packit 994f1a
			 * values.
Packit 994f1a
             */
Packit 994f1a
            tif->tif_data = (tidata_t) _TIFFmalloc(sizeof(LZWCodecState));
Packit 994f1a
            if (tif->tif_data == NULL)
Packit 994f1a
            {
Packit 994f1a
				TIFFErrorExt(tif->tif_clientdata, "LZWPreDecode", "No space for LZW state block");
Packit 994f1a
                return (0);
Packit 994f1a
            }
Packit 994f1a
Packit 994f1a
            DecoderState(tif)->dec_codetab = NULL;
Packit 994f1a
            DecoderState(tif)->dec_decode = NULL;
Packit 994f1a
            
Packit 994f1a
            /*
Packit 994f1a
             * Setup predictor setup.
Packit 994f1a
             */
Packit 994f1a
            (void) TIFFPredictorInit(tif);
Packit 994f1a
Packit 994f1a
            sp = DecoderState(tif);
Packit 994f1a
        }
Packit 994f1a
            
Packit 994f1a
	assert(sp != NULL);
Packit 994f1a
Packit 994f1a
	if (sp->dec_codetab == NULL) {
Packit 994f1a
		sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
Packit 994f1a
		if (sp->dec_codetab == NULL) {
Packit 994f1a
			TIFFErrorExt(tif->tif_clientdata, module,
Packit 994f1a
				     "No space for LZW code table");
Packit 994f1a
			return (0);
Packit 994f1a
		}
Packit 994f1a
		/*
Packit 994f1a
		 * Pre-load the table.
Packit 994f1a
		 */
Packit 994f1a
                code = 255;
Packit 994f1a
                do {
Packit 994f1a
                    sp->dec_codetab[code].value = code;
Packit 994f1a
                    sp->dec_codetab[code].firstchar = code;
Packit 994f1a
                    sp->dec_codetab[code].length = 1;
Packit 994f1a
                    sp->dec_codetab[code].next = NULL;
Packit 994f1a
                } while (code--);
Packit 994f1a
		/*
Packit 994f1a
		 * Zero-out the unused entries
Packit 994f1a
                 */
Packit 994f1a
                 _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
Packit 994f1a
			     (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
Packit 994f1a
	}
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Setup state for decoding a strip.
Packit 994f1a
 */
Packit 994f1a
static int
Packit 994f1a
LZWPreDecode(TIFF* tif, tsample_t s)
Packit 994f1a
{
Packit 994f1a
	LZWCodecState *sp = DecoderState(tif);
Packit 994f1a
Packit 994f1a
	(void) s;
Packit 994f1a
	assert(sp != NULL);
Packit 994f1a
        if( sp->dec_codetab == NULL )
Packit 994f1a
        {
Packit 994f1a
            tif->tif_setupdecode( tif );
Packit 994f1a
        }
Packit 994f1a
Packit 994f1a
	/*
Packit 994f1a
	 * Check for old bit-reversed codes.
Packit 994f1a
	 */
Packit 994f1a
	if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
Packit 994f1a
#ifdef LZW_COMPAT
Packit 994f1a
		if (!sp->dec_decode) {
Packit 994f1a
			TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
			    "Old-style LZW codes, convert file");
Packit 994f1a
			/*
Packit 994f1a
			 * Override default decoding methods with
Packit 994f1a
			 * ones that deal with the old coding.
Packit 994f1a
			 * Otherwise the predictor versions set
Packit 994f1a
			 * above will call the compatibility routines
Packit 994f1a
			 * through the dec_decode method.
Packit 994f1a
			 */
Packit 994f1a
			tif->tif_decoderow = LZWDecodeCompat;
Packit 994f1a
			tif->tif_decodestrip = LZWDecodeCompat;
Packit 994f1a
			tif->tif_decodetile = LZWDecodeCompat;
Packit 994f1a
			/*
Packit 994f1a
			 * If doing horizontal differencing, must
Packit 994f1a
			 * re-setup the predictor logic since we
Packit 994f1a
			 * switched the basic decoder methods...
Packit 994f1a
			 */
Packit 994f1a
			(*tif->tif_setupdecode)(tif);
Packit 994f1a
			sp->dec_decode = LZWDecodeCompat;
Packit 994f1a
		}
Packit 994f1a
		sp->lzw_maxcode = MAXCODE(BITS_MIN);
Packit 994f1a
#else /* !LZW_COMPAT */
Packit 994f1a
		if (!sp->dec_decode) {
Packit 994f1a
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
			    "Old-style LZW codes not supported");
Packit 994f1a
			sp->dec_decode = LZWDecode;
Packit 994f1a
		}
Packit 994f1a
		return (0);
Packit 994f1a
#endif/* !LZW_COMPAT */
Packit 994f1a
	} else {
Packit 994f1a
		sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
Packit 994f1a
		sp->dec_decode = LZWDecode;
Packit 994f1a
	}
Packit 994f1a
	sp->lzw_nbits = BITS_MIN;
Packit 994f1a
	sp->lzw_nextbits = 0;
Packit 994f1a
	sp->lzw_nextdata = 0;
Packit 994f1a
Packit 994f1a
	sp->dec_restart = 0;
Packit 994f1a
	sp->dec_nbitsmask = MAXCODE(BITS_MIN);
Packit 994f1a
#ifdef LZW_CHECKEOS
Packit 994f1a
	sp->dec_bitsleft = tif->tif_rawcc << 3;
Packit 994f1a
#endif
Packit 994f1a
	sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
Packit 994f1a
	/*
Packit 994f1a
	 * Zero entries that are not yet filled in.  We do
Packit 994f1a
	 * this to guard against bogus input data that causes
Packit 994f1a
	 * us to index into undefined entries.  If you can
Packit 994f1a
	 * come up with a way to safely bounds-check input codes
Packit 994f1a
	 * while decoding then you can remove this operation.
Packit 994f1a
	 */
Packit 994f1a
	_TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
Packit 994f1a
	sp->dec_oldcodep = &sp->dec_codetab[-1];
Packit 994f1a
	sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Decode a "hunk of data".
Packit 994f1a
 */
Packit 994f1a
#define	GetNextCode(sp, bp, code) {				\
Packit 994f1a
	nextdata = (nextdata<<8) | *(bp)++;			\
Packit 994f1a
	nextbits += 8;						\
Packit 994f1a
	if (nextbits < nbits) {					\
Packit 994f1a
		nextdata = (nextdata<<8) | *(bp)++;		\
Packit 994f1a
		nextbits += 8;					\
Packit 994f1a
	}							\
Packit 994f1a
	code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask);	\
Packit 994f1a
	nextbits -= nbits;					\
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
static void
Packit 994f1a
codeLoop(TIFF* tif)
Packit 994f1a
{
Packit 994f1a
	TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
	    "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
Packit 994f1a
	    tif->tif_row);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
static int
Packit 994f1a
LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
Packit 994f1a
{
Packit 994f1a
	LZWCodecState *sp = DecoderState(tif);
Packit 994f1a
	char *op = (char*) op0;
Packit 994f1a
	long occ = (long) occ0;
Packit 994f1a
	char *tp;
Packit 994f1a
	unsigned char *bp;
Packit 994f1a
	hcode_t code;
Packit 994f1a
	int len;
Packit 994f1a
	long nbits, nextbits, nextdata, nbitsmask;
Packit 994f1a
	code_t *codep, *free_entp, *maxcodep, *oldcodep;
Packit 994f1a
Packit 994f1a
	(void) s;
Packit 994f1a
	assert(sp != NULL);
Packit 994f1a
        assert(sp->dec_codetab != NULL);
Packit 994f1a
	/*
Packit 994f1a
	 * Restart interrupted output operation.
Packit 994f1a
	 */
Packit 994f1a
	if (sp->dec_restart) {
Packit 994f1a
		long residue;
Packit 994f1a
Packit 994f1a
		codep = sp->dec_codep;
Packit 994f1a
		residue = codep->length - sp->dec_restart;
Packit 994f1a
		if (residue > occ) {
Packit 994f1a
			/*
Packit 994f1a
			 * Residue from previous decode is sufficient
Packit 994f1a
			 * to satisfy decode request.  Skip to the
Packit 994f1a
			 * start of the decoded string, place decoded
Packit 994f1a
			 * values in the output buffer, and return.
Packit 994f1a
			 */
Packit 994f1a
			sp->dec_restart += occ;
Packit 994f1a
			do {
Packit 994f1a
				codep = codep->next;
Packit 994f1a
			} while (--residue > occ && codep);
Packit 994f1a
			if (codep) {
Packit 994f1a
				tp = op + occ;
Packit 994f1a
				do {
Packit 994f1a
					*--tp = codep->value;
Packit 994f1a
					codep = codep->next;
Packit 994f1a
				} while (--occ && codep);
Packit 994f1a
			}
Packit 994f1a
			return (1);
Packit 994f1a
		}
Packit 994f1a
		/*
Packit 994f1a
		 * Residue satisfies only part of the decode request.
Packit 994f1a
		 */
Packit 994f1a
		op += residue, occ -= residue;
Packit 994f1a
		tp = op;
Packit 994f1a
		do {
Packit 994f1a
			int t;
Packit 994f1a
			--tp;
Packit 994f1a
			t = codep->value;
Packit 994f1a
			codep = codep->next;
Packit 994f1a
			*tp = t;
Packit 994f1a
		} while (--residue && codep);
Packit 994f1a
		sp->dec_restart = 0;
Packit 994f1a
	}
Packit 994f1a
Packit 994f1a
	bp = (unsigned char *)tif->tif_rawcp;
Packit 994f1a
	nbits = sp->lzw_nbits;
Packit 994f1a
	nextdata = sp->lzw_nextdata;
Packit 994f1a
	nextbits = sp->lzw_nextbits;
Packit 994f1a
	nbitsmask = sp->dec_nbitsmask;
Packit 994f1a
	oldcodep = sp->dec_oldcodep;
Packit 994f1a
	free_entp = sp->dec_free_entp;
Packit 994f1a
	maxcodep = sp->dec_maxcodep;
Packit 994f1a
Packit 994f1a
	while (occ > 0) {
Packit 994f1a
		NextCode(tif, sp, bp, code, GetNextCode);
Packit 994f1a
		if (code == CODE_EOI)
Packit 994f1a
			break;
Packit 994f1a
		if (code == CODE_CLEAR) {
Packit 994f1a
			free_entp = sp->dec_codetab + CODE_FIRST;
Packit 994f1a
			_TIFFmemset(free_entp, 0,
Packit 994f1a
				    (CSIZE - CODE_FIRST) * sizeof (code_t));
Packit 994f1a
			nbits = BITS_MIN;
Packit 994f1a
			nbitsmask = MAXCODE(BITS_MIN);
Packit 994f1a
			maxcodep = sp->dec_codetab + nbitsmask-1;
Packit 994f1a
			NextCode(tif, sp, bp, code, GetNextCode);
Packit 994f1a
			if (code == CODE_EOI)
Packit 994f1a
				break;
Packit 994f1a
			if (code == CODE_CLEAR) {
Packit 994f1a
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
				"LZWDecode: Corrupted LZW table at scanline %d",
Packit 994f1a
					     tif->tif_row);
Packit 994f1a
				return (0);
Packit 994f1a
			}
Packit 994f1a
			*op++ = (char)code, occ--;
Packit 994f1a
			oldcodep = sp->dec_codetab + code;
Packit 994f1a
			continue;
Packit 994f1a
		}
Packit 994f1a
		codep = sp->dec_codetab + code;
Packit 994f1a
Packit 994f1a
		/*
Packit 994f1a
	 	 * Add the new entry to the code table.
Packit 994f1a
	 	 */
Packit 994f1a
		if (free_entp < &sp->dec_codetab[0] ||
Packit 994f1a
			free_entp >= &sp->dec_codetab[CSIZE]) {
Packit 994f1a
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
			"LZWDecode: Corrupted LZW table at scanline %d",
Packit 994f1a
			tif->tif_row);
Packit 994f1a
			return (0);
Packit 994f1a
		}
Packit 994f1a
Packit 994f1a
		free_entp->next = oldcodep;
Packit 994f1a
		if (free_entp->next < &sp->dec_codetab[0] ||
Packit 994f1a
			free_entp->next >= &sp->dec_codetab[CSIZE]) {
Packit 994f1a
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
			"LZWDecode: Corrupted LZW table at scanline %d",
Packit 994f1a
			tif->tif_row);
Packit 994f1a
			return (0);
Packit 994f1a
		}
Packit 994f1a
		free_entp->firstchar = free_entp->next->firstchar;
Packit 994f1a
		free_entp->length = free_entp->next->length+1;
Packit 994f1a
		free_entp->value = (codep < free_entp) ?
Packit 994f1a
		    codep->firstchar : free_entp->firstchar;
Packit 994f1a
		if (++free_entp > maxcodep) {
Packit 994f1a
			if (++nbits > BITS_MAX)		/* should not happen */
Packit 994f1a
				nbits = BITS_MAX;
Packit 994f1a
			nbitsmask = MAXCODE(nbits);
Packit 994f1a
			maxcodep = sp->dec_codetab + nbitsmask-1;
Packit 994f1a
		}
Packit 994f1a
		oldcodep = codep;
Packit 994f1a
		if (code >= 256) {
Packit 994f1a
			/*
Packit 994f1a
		 	 * Code maps to a string, copy string
Packit 994f1a
			 * value to output (written in reverse).
Packit 994f1a
		 	 */
Packit 994f1a
			if(codep->length == 0) {
Packit 994f1a
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
	    		    "LZWDecode: Wrong length of decoded string: "
Packit 994f1a
			    "data probably corrupted at scanline %d",
Packit 994f1a
			    tif->tif_row);	
Packit 994f1a
			    return (0);
Packit 994f1a
			}
Packit 994f1a
			if (codep->length > occ) {
Packit 994f1a
				/*
Packit 994f1a
				 * String is too long for decode buffer,
Packit 994f1a
				 * locate portion that will fit, copy to
Packit 994f1a
				 * the decode buffer, and setup restart
Packit 994f1a
				 * logic for the next decoding call.
Packit 994f1a
				 */
Packit 994f1a
				sp->dec_codep = codep;
Packit 994f1a
				do {
Packit 994f1a
					codep = codep->next;
Packit 994f1a
				} while (codep && codep->length > occ);
Packit 994f1a
				if (codep) {
Packit 994f1a
					sp->dec_restart = occ;
Packit 994f1a
					tp = op + occ;
Packit 994f1a
					do  {
Packit 994f1a
						*--tp = codep->value;
Packit 994f1a
						codep = codep->next;
Packit 994f1a
					}  while (--occ && codep);
Packit 994f1a
					if (codep)
Packit 994f1a
						codeLoop(tif);
Packit 994f1a
				}
Packit 994f1a
				break;
Packit 994f1a
			}
Packit 994f1a
			len = codep->length;
Packit 994f1a
			tp = op + len;
Packit 994f1a
			do {
Packit 994f1a
				int t;
Packit 994f1a
				--tp;
Packit 994f1a
				t = codep->value;
Packit 994f1a
				codep = codep->next;
Packit 994f1a
				*tp = t;
Packit 994f1a
			} while (codep && tp > op);
Packit 994f1a
			if (codep) {
Packit 994f1a
			    codeLoop(tif);
Packit 994f1a
			    break;
Packit 994f1a
			}
Packit 994f1a
			op += len, occ -= len;
Packit 994f1a
		} else
Packit 994f1a
			*op++ = (char)code, occ--;
Packit 994f1a
	}
Packit 994f1a
Packit 994f1a
	tif->tif_rawcp = (tidata_t) bp;
Packit 994f1a
	sp->lzw_nbits = (unsigned short) nbits;
Packit 994f1a
	sp->lzw_nextdata = nextdata;
Packit 994f1a
	sp->lzw_nextbits = nextbits;
Packit 994f1a
	sp->dec_nbitsmask = nbitsmask;
Packit 994f1a
	sp->dec_oldcodep = oldcodep;
Packit 994f1a
	sp->dec_free_entp = free_entp;
Packit 994f1a
	sp->dec_maxcodep = maxcodep;
Packit 994f1a
Packit 994f1a
	if (occ > 0) {
Packit 994f1a
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
		"LZWDecode: Not enough data at scanline %d (short %ld bytes)",
Packit 994f1a
		    tif->tif_row, occ);
Packit 994f1a
		return (0);
Packit 994f1a
	}
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
#ifdef LZW_COMPAT
Packit 994f1a
/*
Packit 994f1a
 * Decode a "hunk of data" for old images.
Packit 994f1a
 */
Packit 994f1a
#define	GetNextCodeCompat(sp, bp, code) {			\
Packit 994f1a
	nextdata |= (unsigned long) *(bp)++ << nextbits;	\
Packit 994f1a
	nextbits += 8;						\
Packit 994f1a
	if (nextbits < nbits) {					\
Packit 994f1a
		nextdata |= (unsigned long) *(bp)++ << nextbits;\
Packit 994f1a
		nextbits += 8;					\
Packit 994f1a
	}							\
Packit 994f1a
	code = (hcode_t)(nextdata & nbitsmask);			\
Packit 994f1a
	nextdata >>= nbits;					\
Packit 994f1a
	nextbits -= nbits;					\
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
static int
Packit 994f1a
LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
Packit 994f1a
{
Packit 994f1a
	LZWCodecState *sp = DecoderState(tif);
Packit 994f1a
	char *op = (char*) op0;
Packit 994f1a
	long occ = (long) occ0;
Packit 994f1a
	char *tp;
Packit 994f1a
	unsigned char *bp;
Packit 994f1a
	int code, nbits;
Packit 994f1a
	long nextbits, nextdata, nbitsmask;
Packit 994f1a
	code_t *codep, *free_entp, *maxcodep, *oldcodep;
Packit 994f1a
Packit 994f1a
	(void) s;
Packit 994f1a
	assert(sp != NULL);
Packit 994f1a
	/*
Packit 994f1a
	 * Restart interrupted output operation.
Packit 994f1a
	 */
Packit 994f1a
	if (sp->dec_restart) {
Packit 994f1a
		long residue;
Packit 994f1a
Packit 994f1a
		codep = sp->dec_codep;
Packit 994f1a
		residue = codep->length - sp->dec_restart;
Packit 994f1a
		if (residue > occ) {
Packit 994f1a
			/*
Packit 994f1a
			 * Residue from previous decode is sufficient
Packit 994f1a
			 * to satisfy decode request.  Skip to the
Packit 994f1a
			 * start of the decoded string, place decoded
Packit 994f1a
			 * values in the output buffer, and return.
Packit 994f1a
			 */
Packit 994f1a
			sp->dec_restart += occ;
Packit 994f1a
			do {
Packit 994f1a
				codep = codep->next;
Packit 994f1a
			} while (--residue > occ);
Packit 994f1a
			tp = op + occ;
Packit 994f1a
			do {
Packit 994f1a
				*--tp = codep->value;
Packit 994f1a
				codep = codep->next;
Packit 994f1a
			} while (--occ);
Packit 994f1a
			return (1);
Packit 994f1a
		}
Packit 994f1a
		/*
Packit 994f1a
		 * Residue satisfies only part of the decode request.
Packit 994f1a
		 */
Packit 994f1a
		op += residue, occ -= residue;
Packit 994f1a
		tp = op;
Packit 994f1a
		do {
Packit 994f1a
			*--tp = codep->value;
Packit 994f1a
			codep = codep->next;
Packit 994f1a
		} while (--residue);
Packit 994f1a
		sp->dec_restart = 0;
Packit 994f1a
	}
Packit 994f1a
Packit 994f1a
	bp = (unsigned char *)tif->tif_rawcp;
Packit 994f1a
	nbits = sp->lzw_nbits;
Packit 994f1a
	nextdata = sp->lzw_nextdata;
Packit 994f1a
	nextbits = sp->lzw_nextbits;
Packit 994f1a
	nbitsmask = sp->dec_nbitsmask;
Packit 994f1a
	oldcodep = sp->dec_oldcodep;
Packit 994f1a
	free_entp = sp->dec_free_entp;
Packit 994f1a
	maxcodep = sp->dec_maxcodep;
Packit 994f1a
Packit 994f1a
	while (occ > 0) {
Packit 994f1a
		NextCode(tif, sp, bp, code, GetNextCodeCompat);
Packit 994f1a
		if (code == CODE_EOI)
Packit 994f1a
			break;
Packit 994f1a
		if (code == CODE_CLEAR) {
Packit 994f1a
			free_entp = sp->dec_codetab + CODE_FIRST;
Packit 994f1a
			_TIFFmemset(free_entp, 0,
Packit 994f1a
				    (CSIZE - CODE_FIRST) * sizeof (code_t));
Packit 994f1a
			nbits = BITS_MIN;
Packit 994f1a
			nbitsmask = MAXCODE(BITS_MIN);
Packit 994f1a
			maxcodep = sp->dec_codetab + nbitsmask;
Packit 994f1a
			NextCode(tif, sp, bp, code, GetNextCodeCompat);
Packit 994f1a
			if (code == CODE_EOI)
Packit 994f1a
				break;
Packit 994f1a
			if (code == CODE_CLEAR) {
Packit 994f1a
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
				"LZWDecode: Corrupted LZW table at scanline %d",
Packit 994f1a
					     tif->tif_row);
Packit 994f1a
				return (0);
Packit 994f1a
			}
Packit 994f1a
			*op++ = code, occ--;
Packit 994f1a
			oldcodep = sp->dec_codetab + code;
Packit 994f1a
			continue;
Packit 994f1a
		}
Packit 994f1a
		codep = sp->dec_codetab + code;
Packit 994f1a
Packit 994f1a
		/*
Packit 994f1a
	 	 * Add the new entry to the code table.
Packit 994f1a
	 	 */
Packit 994f1a
		if (free_entp < &sp->dec_codetab[0] ||
Packit 994f1a
			free_entp >= &sp->dec_codetab[CSIZE]) {
Packit 994f1a
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
			"LZWDecodeCompat: Corrupted LZW table at scanline %d",
Packit 994f1a
			tif->tif_row);
Packit 994f1a
			return (0);
Packit 994f1a
		}
Packit 994f1a
Packit 994f1a
		free_entp->next = oldcodep;
Packit 994f1a
		if (free_entp->next < &sp->dec_codetab[0] ||
Packit 994f1a
			free_entp->next >= &sp->dec_codetab[CSIZE]) {
Packit 994f1a
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
			"LZWDecodeCompat: Corrupted LZW table at scanline %d",
Packit 994f1a
			tif->tif_row);
Packit 994f1a
			return (0);
Packit 994f1a
		}
Packit 994f1a
		free_entp->firstchar = free_entp->next->firstchar;
Packit 994f1a
		free_entp->length = free_entp->next->length+1;
Packit 994f1a
		free_entp->value = (codep < free_entp) ?
Packit 994f1a
		    codep->firstchar : free_entp->firstchar;
Packit 994f1a
		if (++free_entp > maxcodep) {
Packit 994f1a
			if (++nbits > BITS_MAX)		/* should not happen */
Packit 994f1a
				nbits = BITS_MAX;
Packit 994f1a
			nbitsmask = MAXCODE(nbits);
Packit 994f1a
			maxcodep = sp->dec_codetab + nbitsmask;
Packit 994f1a
		}
Packit 994f1a
		oldcodep = codep;
Packit 994f1a
		if (code >= 256) {
Packit 994f1a
			char *op_orig = op;
Packit 994f1a
			/*
Packit 994f1a
		 	 * Code maps to a string, copy string
Packit 994f1a
			 * value to output (written in reverse).
Packit 994f1a
		 	 */
Packit 994f1a
			if(codep->length == 0) {
Packit 994f1a
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
	    		    "LZWDecodeCompat: Wrong length of decoded "
Packit 994f1a
			    "string: data probably corrupted at scanline %d",
Packit 994f1a
			    tif->tif_row);	
Packit 994f1a
			    return (0);
Packit 994f1a
			}
Packit 994f1a
			if (codep->length > occ) {
Packit 994f1a
				/*
Packit 994f1a
				 * String is too long for decode buffer,
Packit 994f1a
				 * locate portion that will fit, copy to
Packit 994f1a
				 * the decode buffer, and setup restart
Packit 994f1a
				 * logic for the next decoding call.
Packit 994f1a
				 */
Packit 994f1a
				sp->dec_codep = codep;
Packit 994f1a
				do {
Packit 994f1a
					codep = codep->next;
Packit 994f1a
				} while (codep->length > occ);
Packit 994f1a
				sp->dec_restart = occ;
Packit 994f1a
				tp = op + occ;
Packit 994f1a
				do  {
Packit 994f1a
					*--tp = codep->value;
Packit 994f1a
					codep = codep->next;
Packit 994f1a
				}  while (--occ);
Packit 994f1a
				break;
Packit 994f1a
			}
Packit 994f1a
			op += codep->length, occ -= codep->length;
Packit 994f1a
			tp = op;
Packit 994f1a
			do {
Packit 994f1a
				*--tp = codep->value;
Packit 994f1a
			} while( (codep = codep->next) != NULL && tp > op_orig);
Packit 994f1a
		} else
Packit 994f1a
			*op++ = code, occ--;
Packit 994f1a
	}
Packit 994f1a
Packit 994f1a
	tif->tif_rawcp = (tidata_t) bp;
Packit 994f1a
	sp->lzw_nbits = nbits;
Packit 994f1a
	sp->lzw_nextdata = nextdata;
Packit 994f1a
	sp->lzw_nextbits = nextbits;
Packit 994f1a
	sp->dec_nbitsmask = nbitsmask;
Packit 994f1a
	sp->dec_oldcodep = oldcodep;
Packit 994f1a
	sp->dec_free_entp = free_entp;
Packit 994f1a
	sp->dec_maxcodep = maxcodep;
Packit 994f1a
Packit 994f1a
	if (occ > 0) {
Packit 994f1a
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 994f1a
	    "LZWDecodeCompat: Not enough data at scanline %d (short %ld bytes)",
Packit 994f1a
		    tif->tif_row, occ);
Packit 994f1a
		return (0);
Packit 994f1a
	}
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
#endif /* LZW_COMPAT */
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * LZW Encoding.
Packit 994f1a
 */
Packit 994f1a
Packit 994f1a
static int
Packit 994f1a
LZWSetupEncode(TIFF* tif)
Packit 994f1a
{
Packit 994f1a
	LZWCodecState* sp = EncoderState(tif);
Packit 994f1a
	static const char module[] = "LZWSetupEncode";
Packit 994f1a
Packit 994f1a
	assert(sp != NULL);
Packit 994f1a
	sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
Packit 994f1a
	if (sp->enc_hashtab == NULL) {
Packit 994f1a
		TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW hash table");
Packit 994f1a
		return (0);
Packit 994f1a
	}
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Reset encoding state at the start of a strip.
Packit 994f1a
 */
Packit 994f1a
static int
Packit 994f1a
LZWPreEncode(TIFF* tif, tsample_t s)
Packit 994f1a
{
Packit 994f1a
	LZWCodecState *sp = EncoderState(tif);
Packit 994f1a
Packit 994f1a
	(void) s;
Packit 994f1a
	assert(sp != NULL);
Packit 994f1a
        
Packit 994f1a
        if( sp->enc_hashtab == NULL )
Packit 994f1a
        {
Packit 994f1a
            tif->tif_setupencode( tif );
Packit 994f1a
        }
Packit 994f1a
Packit 994f1a
	sp->lzw_nbits = BITS_MIN;
Packit 994f1a
	sp->lzw_maxcode = MAXCODE(BITS_MIN);
Packit 994f1a
	sp->lzw_free_ent = CODE_FIRST;
Packit 994f1a
	sp->lzw_nextbits = 0;
Packit 994f1a
	sp->lzw_nextdata = 0;
Packit 994f1a
	sp->enc_checkpoint = CHECK_GAP;
Packit 994f1a
	sp->enc_ratio = 0;
Packit 994f1a
	sp->enc_incount = 0;
Packit 994f1a
	sp->enc_outcount = 0;
Packit 994f1a
	/*
Packit 994f1a
	 * The 4 here insures there is space for 2 max-sized
Packit 994f1a
	 * codes in LZWEncode and LZWPostDecode.
Packit 994f1a
	 */
Packit 994f1a
	sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
Packit 994f1a
	cl_hash(sp);		/* clear hash table */
Packit 994f1a
	sp->enc_oldcode = (hcode_t) -1;	/* generates CODE_CLEAR in LZWEncode */
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
#define	CALCRATIO(sp, rat) {					\
Packit 994f1a
	if (incount > 0x007fffff) { /* NB: shift will overflow */\
Packit 994f1a
		rat = outcount >> 8;				\
Packit 994f1a
		rat = (rat == 0 ? 0x7fffffff : incount/rat);	\
Packit 994f1a
	} else							\
Packit 994f1a
		rat = (incount<<8) / outcount;			\
Packit 994f1a
}
Packit 994f1a
#define	PutNextCode(op, c) {					\
Packit 994f1a
	nextdata = (nextdata << nbits) | c;			\
Packit 994f1a
	nextbits += nbits;					\
Packit 994f1a
	*op++ = (unsigned char)(nextdata >> (nextbits-8));		\
Packit 994f1a
	nextbits -= 8;						\
Packit 994f1a
	if (nextbits >= 8) {					\
Packit 994f1a
		*op++ = (unsigned char)(nextdata >> (nextbits-8));	\
Packit 994f1a
		nextbits -= 8;					\
Packit 994f1a
	}							\
Packit 994f1a
	outcount += nbits;					\
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Encode a chunk of pixels.
Packit 994f1a
 *
Packit 994f1a
 * Uses an open addressing double hashing (no chaining) on the 
Packit 994f1a
 * prefix code/next character combination.  We do a variant of
Packit 994f1a
 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
Packit 994f1a
 * relatively-prime secondary probe.  Here, the modular division
Packit 994f1a
 * first probe is gives way to a faster exclusive-or manipulation. 
Packit 994f1a
 * Also do block compression with an adaptive reset, whereby the
Packit 994f1a
 * code table is cleared when the compression ratio decreases,
Packit 994f1a
 * but after the table fills.  The variable-length output codes
Packit 994f1a
 * are re-sized at this point, and a CODE_CLEAR is generated
Packit 994f1a
 * for the decoder. 
Packit 994f1a
 */
Packit 994f1a
static int
Packit 994f1a
LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
Packit 994f1a
{
Packit 994f1a
	register LZWCodecState *sp = EncoderState(tif);
Packit 994f1a
	register long fcode;
Packit 994f1a
	register hash_t *hp;
Packit 994f1a
	register int h, c;
Packit 994f1a
	hcode_t ent;
Packit 994f1a
	long disp;
Packit 994f1a
	long incount, outcount, checkpoint;
Packit 994f1a
	long nextdata, nextbits;
Packit 994f1a
	int free_ent, maxcode, nbits;
Packit 994f1a
	tidata_t op, limit;
Packit 994f1a
Packit 994f1a
	(void) s;
Packit 994f1a
	if (sp == NULL)
Packit 994f1a
		return (0);
Packit 994f1a
Packit 994f1a
        assert(sp->enc_hashtab != NULL);
Packit 994f1a
Packit 994f1a
	/*
Packit 994f1a
	 * Load local state.
Packit 994f1a
	 */
Packit 994f1a
	incount = sp->enc_incount;
Packit 994f1a
	outcount = sp->enc_outcount;
Packit 994f1a
	checkpoint = sp->enc_checkpoint;
Packit 994f1a
	nextdata = sp->lzw_nextdata;
Packit 994f1a
	nextbits = sp->lzw_nextbits;
Packit 994f1a
	free_ent = sp->lzw_free_ent;
Packit 994f1a
	maxcode = sp->lzw_maxcode;
Packit 994f1a
	nbits = sp->lzw_nbits;
Packit 994f1a
	op = tif->tif_rawcp;
Packit 994f1a
	limit = sp->enc_rawlimit;
Packit 994f1a
	ent = sp->enc_oldcode;
Packit 994f1a
Packit 994f1a
	if (ent == (hcode_t) -1 && cc > 0) {
Packit 994f1a
		/*
Packit 994f1a
		 * NB: This is safe because it can only happen
Packit 994f1a
		 *     at the start of a strip where we know there
Packit 994f1a
		 *     is space in the data buffer.
Packit 994f1a
		 */
Packit 994f1a
		PutNextCode(op, CODE_CLEAR);
Packit 994f1a
		ent = *bp++; cc--; incount++;
Packit 994f1a
	}
Packit 994f1a
	while (cc > 0) {
Packit 994f1a
		c = *bp++; cc--; incount++;
Packit 994f1a
		fcode = ((long)c << BITS_MAX) + ent;
Packit 994f1a
		h = (c << HSHIFT) ^ ent;	/* xor hashing */
Packit 994f1a
#ifdef _WINDOWS
Packit 994f1a
		/*
Packit 994f1a
		 * Check hash index for an overflow.
Packit 994f1a
		 */
Packit 994f1a
		if (h >= HSIZE)
Packit 994f1a
			h -= HSIZE;
Packit 994f1a
#endif
Packit 994f1a
		hp = &sp->enc_hashtab[h];
Packit 994f1a
		if (hp->hash == fcode) {
Packit 994f1a
			ent = hp->code;
Packit 994f1a
			continue;
Packit 994f1a
		}
Packit 994f1a
		if (hp->hash >= 0) {
Packit 994f1a
			/*
Packit 994f1a
			 * Primary hash failed, check secondary hash.
Packit 994f1a
			 */
Packit 994f1a
			disp = HSIZE - h;
Packit 994f1a
			if (h == 0)
Packit 994f1a
				disp = 1;
Packit 994f1a
			do {
Packit 994f1a
				/*
Packit 994f1a
				 * Avoid pointer arithmetic 'cuz of
Packit 994f1a
				 * wraparound problems with segments.
Packit 994f1a
				 */
Packit 994f1a
				if ((h -= disp) < 0)
Packit 994f1a
					h += HSIZE;
Packit 994f1a
				hp = &sp->enc_hashtab[h];
Packit 994f1a
				if (hp->hash == fcode) {
Packit 994f1a
					ent = hp->code;
Packit 994f1a
					goto hit;
Packit 994f1a
				}
Packit 994f1a
			} while (hp->hash >= 0);
Packit 994f1a
		}
Packit 994f1a
		/*
Packit 994f1a
		 * New entry, emit code and add to table.
Packit 994f1a
		 */
Packit 994f1a
		/*
Packit 994f1a
		 * Verify there is space in the buffer for the code
Packit 994f1a
		 * and any potential Clear code that might be emitted
Packit 994f1a
		 * below.  The value of limit is setup so that there
Packit 994f1a
		 * are at least 4 bytes free--room for 2 codes.
Packit 994f1a
		 */
Packit 994f1a
		if (op > limit) {
Packit 994f1a
			tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
Packit 994f1a
			TIFFFlushData1(tif);
Packit 994f1a
			op = tif->tif_rawdata;
Packit 994f1a
		}
Packit 994f1a
		PutNextCode(op, ent);
Packit 994f1a
		ent = c;
Packit 994f1a
		hp->code = free_ent++;
Packit 994f1a
		hp->hash = fcode;
Packit 994f1a
		if (free_ent == CODE_MAX-1) {
Packit 994f1a
			/* table is full, emit clear code and reset */
Packit 994f1a
			cl_hash(sp);
Packit 994f1a
			sp->enc_ratio = 0;
Packit 994f1a
			incount = 0;
Packit 994f1a
			outcount = 0;
Packit 994f1a
			free_ent = CODE_FIRST;
Packit 994f1a
			PutNextCode(op, CODE_CLEAR);
Packit 994f1a
			nbits = BITS_MIN;
Packit 994f1a
			maxcode = MAXCODE(BITS_MIN);
Packit 994f1a
		} else {
Packit 994f1a
			/*
Packit 994f1a
			 * If the next entry is going to be too big for
Packit 994f1a
			 * the code size, then increase it, if possible.
Packit 994f1a
			 */
Packit 994f1a
			if (free_ent > maxcode) {
Packit 994f1a
				nbits++;
Packit 994f1a
				assert(nbits <= BITS_MAX);
Packit 994f1a
				maxcode = (int) MAXCODE(nbits);
Packit 994f1a
			} else if (incount >= checkpoint) {
Packit 994f1a
				long rat;
Packit 994f1a
				/*
Packit 994f1a
				 * Check compression ratio and, if things seem
Packit 994f1a
				 * to be slipping, clear the hash table and
Packit 994f1a
				 * reset state.  The compression ratio is a
Packit 994f1a
				 * 24+8-bit fractional number.
Packit 994f1a
				 */
Packit 994f1a
				checkpoint = incount+CHECK_GAP;
Packit 994f1a
				CALCRATIO(sp, rat);
Packit 994f1a
				if (rat <= sp->enc_ratio) {
Packit 994f1a
					cl_hash(sp);
Packit 994f1a
					sp->enc_ratio = 0;
Packit 994f1a
					incount = 0;
Packit 994f1a
					outcount = 0;
Packit 994f1a
					free_ent = CODE_FIRST;
Packit 994f1a
					PutNextCode(op, CODE_CLEAR);
Packit 994f1a
					nbits = BITS_MIN;
Packit 994f1a
					maxcode = MAXCODE(BITS_MIN);
Packit 994f1a
				} else
Packit 994f1a
					sp->enc_ratio = rat;
Packit 994f1a
			}
Packit 994f1a
		}
Packit 994f1a
	hit:
Packit 994f1a
		;
Packit 994f1a
	}
Packit 994f1a
Packit 994f1a
	/*
Packit 994f1a
	 * Restore global state.
Packit 994f1a
	 */
Packit 994f1a
	sp->enc_incount = incount;
Packit 994f1a
	sp->enc_outcount = outcount;
Packit 994f1a
	sp->enc_checkpoint = checkpoint;
Packit 994f1a
	sp->enc_oldcode = ent;
Packit 994f1a
	sp->lzw_nextdata = nextdata;
Packit 994f1a
	sp->lzw_nextbits = nextbits;
Packit 994f1a
	sp->lzw_free_ent = free_ent;
Packit 994f1a
	sp->lzw_maxcode = maxcode;
Packit 994f1a
	sp->lzw_nbits = nbits;
Packit 994f1a
	tif->tif_rawcp = op;
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Finish off an encoded strip by flushing the last
Packit 994f1a
 * string and tacking on an End Of Information code.
Packit 994f1a
 */
Packit 994f1a
static int
Packit 994f1a
LZWPostEncode(TIFF* tif)
Packit 994f1a
{
Packit 994f1a
	register LZWCodecState *sp = EncoderState(tif);
Packit 994f1a
	tidata_t op = tif->tif_rawcp;
Packit 994f1a
	long nextbits = sp->lzw_nextbits;
Packit 994f1a
	long nextdata = sp->lzw_nextdata;
Packit 994f1a
	long outcount = sp->enc_outcount;
Packit 994f1a
	int nbits = sp->lzw_nbits;
Packit 994f1a
Packit 994f1a
	if (op > sp->enc_rawlimit) {
Packit 994f1a
		tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
Packit 994f1a
		TIFFFlushData1(tif);
Packit 994f1a
		op = tif->tif_rawdata;
Packit 994f1a
	}
Packit 994f1a
	if (sp->enc_oldcode != (hcode_t) -1) {
Packit 994f1a
		PutNextCode(op, sp->enc_oldcode);
Packit 994f1a
		sp->enc_oldcode = (hcode_t) -1;
Packit 994f1a
	}
Packit 994f1a
	PutNextCode(op, CODE_EOI);
Packit 994f1a
	if (nextbits > 0) 
Packit 994f1a
		*op++ = (unsigned char)(nextdata << (8-nextbits));
Packit 994f1a
	tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
Packit 994f1a
	return (1);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Reset encoding hash table.
Packit 994f1a
 */
Packit 994f1a
static void
Packit 994f1a
cl_hash(LZWCodecState* sp)
Packit 994f1a
{
Packit 994f1a
	register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
Packit 994f1a
	register long i = HSIZE-8;
Packit 994f1a
Packit 994f1a
 	do {
Packit 994f1a
		i -= 8;
Packit 994f1a
		hp[-7].hash = -1;
Packit 994f1a
		hp[-6].hash = -1;
Packit 994f1a
		hp[-5].hash = -1;
Packit 994f1a
		hp[-4].hash = -1;
Packit 994f1a
		hp[-3].hash = -1;
Packit 994f1a
		hp[-2].hash = -1;
Packit 994f1a
		hp[-1].hash = -1;
Packit 994f1a
		hp[ 0].hash = -1;
Packit 994f1a
		hp -= 8;
Packit 994f1a
	} while (i >= 0);
Packit 994f1a
    	for (i += 8; i > 0; i--, hp--)
Packit 994f1a
		hp->hash = -1;
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
static void
Packit 994f1a
LZWCleanup(TIFF* tif)
Packit 994f1a
{
Packit 994f1a
	(void)TIFFPredictorCleanup(tif);
Packit 994f1a
Packit 994f1a
	assert(tif->tif_data != 0);
Packit 994f1a
Packit 994f1a
	if (DecoderState(tif)->dec_codetab)
Packit 994f1a
		_TIFFfree(DecoderState(tif)->dec_codetab);
Packit 994f1a
Packit 994f1a
	if (EncoderState(tif)->enc_hashtab)
Packit 994f1a
		_TIFFfree(EncoderState(tif)->enc_hashtab);
Packit 994f1a
Packit 994f1a
	_TIFFfree(tif->tif_data);
Packit 994f1a
	tif->tif_data = NULL;
Packit 994f1a
Packit 994f1a
	_TIFFSetDefaultCompressionState(tif);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
int
Packit 994f1a
TIFFInitLZW(TIFF* tif, int scheme)
Packit 994f1a
{
Packit 994f1a
	assert(scheme == COMPRESSION_LZW);
Packit 994f1a
	/*
Packit 994f1a
	 * Allocate state block so tag methods have storage to record values.
Packit 994f1a
	 */
Packit 994f1a
	tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LZWCodecState));
Packit 994f1a
	if (tif->tif_data == NULL)
Packit 994f1a
		goto bad;
Packit 994f1a
	DecoderState(tif)->dec_codetab = NULL;
Packit 994f1a
	DecoderState(tif)->dec_decode = NULL;
Packit 994f1a
	EncoderState(tif)->enc_hashtab = NULL;
Packit 994f1a
        LZWState(tif)->rw_mode = tif->tif_mode;
Packit 994f1a
Packit 994f1a
	/*
Packit 994f1a
	 * Install codec methods.
Packit 994f1a
	 */
Packit 994f1a
	tif->tif_setupdecode = LZWSetupDecode;
Packit 994f1a
	tif->tif_predecode = LZWPreDecode;
Packit 994f1a
	tif->tif_decoderow = LZWDecode;
Packit 994f1a
	tif->tif_decodestrip = LZWDecode;
Packit 994f1a
	tif->tif_decodetile = LZWDecode;
Packit 994f1a
	tif->tif_setupencode = LZWSetupEncode;
Packit 994f1a
	tif->tif_preencode = LZWPreEncode;
Packit 994f1a
	tif->tif_postencode = LZWPostEncode;
Packit 994f1a
	tif->tif_encoderow = LZWEncode;
Packit 994f1a
	tif->tif_encodestrip = LZWEncode;
Packit 994f1a
	tif->tif_encodetile = LZWEncode;
Packit 994f1a
	tif->tif_cleanup = LZWCleanup;
Packit 994f1a
	/*
Packit 994f1a
	 * Setup predictor setup.
Packit 994f1a
	 */
Packit 994f1a
	(void) TIFFPredictorInit(tif);
Packit 994f1a
	return (1);
Packit 994f1a
bad:
Packit 994f1a
	TIFFErrorExt(tif->tif_clientdata, "TIFFInitLZW", 
Packit 994f1a
		     "No space for LZW state block");
Packit 994f1a
	return (0);
Packit 994f1a
}
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Copyright (c) 1985, 1986 The Regents of the University of California.
Packit 994f1a
 * All rights reserved.
Packit 994f1a
 *
Packit 994f1a
 * This code is derived from software contributed to Berkeley by
Packit 994f1a
 * James A. Woods, derived from original work by Spencer Thomas
Packit 994f1a
 * and Joseph Orost.
Packit 994f1a
 *
Packit 994f1a
 * Redistribution and use in source and binary forms are permitted
Packit 994f1a
 * provided that the above copyright notice and this paragraph are
Packit 994f1a
 * duplicated in all such forms and that any documentation,
Packit 994f1a
 * advertising materials, and other materials related to such
Packit 994f1a
 * distribution and use acknowledge that the software was developed
Packit 994f1a
 * by the University of California, Berkeley.  The name of the
Packit 994f1a
 * University may not be used to endorse or promote products derived
Packit 994f1a
 * from this software without specific prior written permission.
Packit 994f1a
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Packit 994f1a
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Packit 994f1a
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit 994f1a
 */
Packit 994f1a
#endif /* LZW_SUPPORT */
Packit 994f1a
Packit 994f1a
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 994f1a
/*
Packit 994f1a
 * Local Variables:
Packit 994f1a
 * mode: c
Packit 994f1a
 * c-basic-offset: 8
Packit 994f1a
 * fill-column: 78
Packit 994f1a
 * End:
Packit 994f1a
 */