Blame libtiff/tif_lzw.c

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