Blame libtiff/tif_lzw.c

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