xref: /reactos/dll/3rdparty/libtiff/tif_lzw.c (revision f87faf67)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Copyright (c) 1988-1997 Sam Leffler
3c2c66affSColin Finck  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4c2c66affSColin Finck  *
5c2c66affSColin Finck  * Permission to use, copy, modify, distribute, and sell this software and
6c2c66affSColin Finck  * its documentation for any purpose is hereby granted without fee, provided
7c2c66affSColin Finck  * that (i) the above copyright notices and this permission notice appear in
8c2c66affSColin Finck  * all copies of the software and related documentation, and (ii) the names of
9c2c66affSColin Finck  * Sam Leffler and Silicon Graphics may not be used in any advertising or
10c2c66affSColin Finck  * publicity relating to the software without the specific, prior written
11c2c66affSColin Finck  * permission of Sam Leffler and Silicon Graphics.
12c2c66affSColin Finck  *
13c2c66affSColin Finck  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14c2c66affSColin Finck  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15c2c66affSColin Finck  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16c2c66affSColin Finck  *
17c2c66affSColin Finck  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18c2c66affSColin Finck  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19c2c66affSColin Finck  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20c2c66affSColin Finck  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21c2c66affSColin Finck  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22c2c66affSColin Finck  * OF THIS SOFTWARE.
23c2c66affSColin Finck  */
24c2c66affSColin Finck 
25c2c66affSColin Finck #include <precomp.h>
26c2c66affSColin Finck #ifdef LZW_SUPPORT
27c2c66affSColin Finck /*
28c2c66affSColin Finck  * TIFF Library.
29c2c66affSColin Finck  * Rev 5.0 Lempel-Ziv & Welch Compression Support
30c2c66affSColin Finck  *
31c2c66affSColin Finck  * This code is derived from the compress program whose code is
32c2c66affSColin Finck  * derived from software contributed to Berkeley by James A. Woods,
33c2c66affSColin Finck  * derived from original work by Spencer Thomas and Joseph Orost.
34c2c66affSColin Finck  *
35c2c66affSColin Finck  * The original Berkeley copyright notice appears below in its entirety.
36c2c66affSColin Finck  */
37c2c66affSColin Finck #include "tif_predict.h"
38c2c66affSColin Finck 
39c2c66affSColin Finck #include <stdio.h>
40c2c66affSColin Finck 
41c2c66affSColin Finck /*
42c2c66affSColin Finck  * NB: The 5.0 spec describes a different algorithm than Aldus
43c2c66affSColin Finck  *     implements.  Specifically, Aldus does code length transitions
44c2c66affSColin Finck  *     one code earlier than should be done (for real LZW).
45c2c66affSColin Finck  *     Earlier versions of this library implemented the correct
46c2c66affSColin Finck  *     LZW algorithm, but emitted codes in a bit order opposite
47c2c66affSColin Finck  *     to the TIFF spec.  Thus, to maintain compatibility w/ Aldus
48c2c66affSColin Finck  *     we interpret MSB-LSB ordered codes to be images written w/
49c2c66affSColin Finck  *     old versions of this library, but otherwise adhere to the
50c2c66affSColin Finck  *     Aldus "off by one" algorithm.
51c2c66affSColin Finck  *
52c2c66affSColin Finck  * Future revisions to the TIFF spec are expected to "clarify this issue".
53c2c66affSColin Finck  */
54c2c66affSColin Finck #define LZW_COMPAT              /* include backwards compatibility code */
55c2c66affSColin Finck /*
56c2c66affSColin Finck  * Each strip of data is supposed to be terminated by a CODE_EOI.
57c2c66affSColin Finck  * If the following #define is included, the decoder will also
58c2c66affSColin Finck  * check for end-of-strip w/o seeing this code.  This makes the
59c2c66affSColin Finck  * library more robust, but also slower.
60c2c66affSColin Finck  */
61c2c66affSColin Finck #define LZW_CHECKEOS            /* include checks for strips w/o EOI code */
62c2c66affSColin Finck 
63c2c66affSColin Finck #define MAXCODE(n)	((1L<<(n))-1)
64c2c66affSColin Finck /*
65c2c66affSColin Finck  * The TIFF spec specifies that encoded bit
66c2c66affSColin Finck  * strings range from 9 to 12 bits.
67c2c66affSColin Finck  */
68c2c66affSColin Finck #define BITS_MIN        9               /* start with 9 bits */
69c2c66affSColin Finck #define BITS_MAX        12              /* max of 12 bit strings */
70c2c66affSColin Finck /* predefined codes */
71c2c66affSColin Finck #define CODE_CLEAR      256             /* code to clear string table */
72c2c66affSColin Finck #define CODE_EOI        257             /* end-of-information code */
73c2c66affSColin Finck #define CODE_FIRST      258             /* first free code entry */
74c2c66affSColin Finck #define CODE_MAX        MAXCODE(BITS_MAX)
75c2c66affSColin Finck #define HSIZE           9001L           /* 91% occupancy */
76c2c66affSColin Finck #define HSHIFT          (13-8)
77c2c66affSColin Finck #ifdef LZW_COMPAT
78c2c66affSColin Finck /* NB: +1024 is for compatibility with old files */
79c2c66affSColin Finck #define CSIZE           (MAXCODE(BITS_MAX)+1024L)
80c2c66affSColin Finck #else
81c2c66affSColin Finck #define CSIZE           (MAXCODE(BITS_MAX)+1L)
82c2c66affSColin Finck #endif
83c2c66affSColin Finck 
84c2c66affSColin Finck /*
85c2c66affSColin Finck  * State block for each open TIFF file using LZW
86c2c66affSColin Finck  * compression/decompression.  Note that the predictor
87c2c66affSColin Finck  * state block must be first in this data structure.
88c2c66affSColin Finck  */
89c2c66affSColin Finck typedef struct {
90c2c66affSColin Finck 	TIFFPredictorState predict;     /* predictor super class */
91c2c66affSColin Finck 
92c2c66affSColin Finck 	unsigned short  nbits;          /* # of bits/code */
93c2c66affSColin Finck 	unsigned short  maxcode;        /* maximum code for lzw_nbits */
94c2c66affSColin Finck 	unsigned short  free_ent;       /* next free entry in hash table */
95c2c66affSColin Finck 	unsigned long   nextdata;       /* next bits of i/o */
96c2c66affSColin Finck 	long            nextbits;       /* # of valid bits in lzw_nextdata */
97c2c66affSColin Finck 
98c2c66affSColin Finck 	int             rw_mode;        /* preserve rw_mode from init */
99c2c66affSColin Finck } LZWBaseState;
100c2c66affSColin Finck 
101c2c66affSColin Finck #define lzw_nbits       base.nbits
102c2c66affSColin Finck #define lzw_maxcode     base.maxcode
103c2c66affSColin Finck #define lzw_free_ent    base.free_ent
104c2c66affSColin Finck #define lzw_nextdata    base.nextdata
105c2c66affSColin Finck #define lzw_nextbits    base.nextbits
106c2c66affSColin Finck 
107c2c66affSColin Finck /*
108c2c66affSColin Finck  * Encoding-specific state.
109c2c66affSColin Finck  */
110c2c66affSColin Finck typedef uint16 hcode_t;			/* codes fit in 16 bits */
111c2c66affSColin Finck typedef struct {
112c2c66affSColin Finck 	long	hash;
113c2c66affSColin Finck 	hcode_t	code;
114c2c66affSColin Finck } hash_t;
115c2c66affSColin Finck 
116c2c66affSColin Finck /*
117c2c66affSColin Finck  * Decoding-specific state.
118c2c66affSColin Finck  */
119c2c66affSColin Finck typedef struct code_ent {
120c2c66affSColin Finck 	struct code_ent *next;
121c2c66affSColin Finck 	unsigned short	length;		/* string len, including this token */
122c2c66affSColin Finck 	unsigned char	value;		/* data value */
123c2c66affSColin Finck 	unsigned char	firstchar;	/* first token of string */
124c2c66affSColin Finck } code_t;
125c2c66affSColin Finck 
126c2c66affSColin Finck typedef int (*decodeFunc)(TIFF*, uint8*, tmsize_t, uint16);
127c2c66affSColin Finck 
128c2c66affSColin Finck typedef struct {
129c2c66affSColin Finck 	LZWBaseState base;
130c2c66affSColin Finck 
131c2c66affSColin Finck 	/* Decoding specific data */
132c2c66affSColin Finck 	long    dec_nbitsmask;		/* lzw_nbits 1 bits, right adjusted */
133c2c66affSColin Finck 	long    dec_restart;		/* restart count */
134c2c66affSColin Finck #ifdef LZW_CHECKEOS
135c2c66affSColin Finck 	uint64  dec_bitsleft;		/* available bits in raw data */
136743951ecSThomas Faber 	tmsize_t old_tif_rawcc;         /* value of tif_rawcc at the end of the previous TIFLZWDecode() call */
137c2c66affSColin Finck #endif
138c2c66affSColin Finck 	decodeFunc dec_decode;		/* regular or backwards compatible */
139c2c66affSColin Finck 	code_t* dec_codep;		/* current recognized code */
140c2c66affSColin Finck 	code_t* dec_oldcodep;		/* previously recognized code */
141c2c66affSColin Finck 	code_t* dec_free_entp;		/* next free entry */
142c2c66affSColin Finck 	code_t* dec_maxcodep;		/* max available entry */
143c2c66affSColin Finck 	code_t* dec_codetab;		/* kept separate for small machines */
144c2c66affSColin Finck 
145c2c66affSColin Finck 	/* Encoding specific data */
146c2c66affSColin Finck 	int     enc_oldcode;		/* last code encountered */
147c2c66affSColin Finck 	long    enc_checkpoint;		/* point at which to clear table */
148c2c66affSColin Finck #define CHECK_GAP	10000		/* enc_ratio check interval */
149c2c66affSColin Finck 	long    enc_ratio;		/* current compression ratio */
150c2c66affSColin Finck 	long    enc_incount;		/* (input) data bytes encoded */
151c2c66affSColin Finck 	long    enc_outcount;		/* encoded (output) bytes */
152c2c66affSColin Finck 	uint8*  enc_rawlimit;		/* bound on tif_rawdata buffer */
153c2c66affSColin Finck 	hash_t* enc_hashtab;		/* kept separate for small machines */
154c2c66affSColin Finck } LZWCodecState;
155c2c66affSColin Finck 
156c2c66affSColin Finck #define LZWState(tif)		((LZWBaseState*) (tif)->tif_data)
157c2c66affSColin Finck #define DecoderState(tif)	((LZWCodecState*) LZWState(tif))
158c2c66affSColin Finck #define EncoderState(tif)	((LZWCodecState*) LZWState(tif))
159c2c66affSColin Finck 
160c2c66affSColin Finck static int LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
161c2c66affSColin Finck #ifdef LZW_COMPAT
162c2c66affSColin Finck static int LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
163c2c66affSColin Finck #endif
164c2c66affSColin Finck static void cl_hash(LZWCodecState*);
165c2c66affSColin Finck 
166c2c66affSColin Finck /*
167c2c66affSColin Finck  * LZW Decoder.
168c2c66affSColin Finck  */
169c2c66affSColin Finck 
170c2c66affSColin Finck #ifdef LZW_CHECKEOS
171c2c66affSColin Finck /*
172c2c66affSColin Finck  * This check shouldn't be necessary because each
173c2c66affSColin Finck  * strip is suppose to be terminated with CODE_EOI.
174c2c66affSColin Finck  */
175c2c66affSColin Finck #define	NextCode(_tif, _sp, _bp, _code, _get) {				\
176c2c66affSColin Finck 	if ((_sp)->dec_bitsleft < (uint64)nbits) {			\
177c2c66affSColin Finck 		TIFFWarningExt(_tif->tif_clientdata, module,		\
178c2c66affSColin Finck 		    "LZWDecode: Strip %d not terminated with EOI code", \
179c2c66affSColin Finck 		    _tif->tif_curstrip);				\
180c2c66affSColin Finck 		_code = CODE_EOI;					\
181c2c66affSColin Finck 	} else {							\
182c2c66affSColin Finck 		_get(_sp,_bp,_code);					\
183c2c66affSColin Finck 		(_sp)->dec_bitsleft -= nbits;				\
184c2c66affSColin Finck 	}								\
185c2c66affSColin Finck }
186c2c66affSColin Finck #else
187c2c66affSColin Finck #define	NextCode(tif, sp, bp, code, get) get(sp, bp, code)
188c2c66affSColin Finck #endif
189c2c66affSColin Finck 
190c2c66affSColin Finck static int
LZWFixupTags(TIFF * tif)191c2c66affSColin Finck LZWFixupTags(TIFF* tif)
192c2c66affSColin Finck {
193c2c66affSColin Finck 	(void) tif;
194c2c66affSColin Finck 	return (1);
195c2c66affSColin Finck }
196c2c66affSColin Finck 
197c2c66affSColin Finck static int
LZWSetupDecode(TIFF * tif)198c2c66affSColin Finck LZWSetupDecode(TIFF* tif)
199c2c66affSColin Finck {
200c2c66affSColin Finck 	static const char module[] = "LZWSetupDecode";
201c2c66affSColin Finck 	LZWCodecState* sp = DecoderState(tif);
202c2c66affSColin Finck 	int code;
203c2c66affSColin Finck 
204c2c66affSColin Finck 	if( sp == NULL )
205c2c66affSColin Finck 	{
206c2c66affSColin Finck 		/*
207c2c66affSColin Finck 		 * Allocate state block so tag methods have storage to record
208c2c66affSColin Finck 		 * values.
209c2c66affSColin Finck 		*/
210c2c66affSColin Finck 		tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZWCodecState));
211c2c66affSColin Finck 		if (tif->tif_data == NULL)
212c2c66affSColin Finck 		{
213c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW state block");
214c2c66affSColin Finck 			return (0);
215c2c66affSColin Finck 		}
216c2c66affSColin Finck 
217c2c66affSColin Finck 		DecoderState(tif)->dec_codetab = NULL;
218c2c66affSColin Finck 		DecoderState(tif)->dec_decode = NULL;
219c2c66affSColin Finck 
220c2c66affSColin Finck 		/*
221c2c66affSColin Finck 		 * Setup predictor setup.
222c2c66affSColin Finck 		 */
223c2c66affSColin Finck 		(void) TIFFPredictorInit(tif);
224c2c66affSColin Finck 
225c2c66affSColin Finck 		sp = DecoderState(tif);
226c2c66affSColin Finck 	}
227c2c66affSColin Finck 
228c2c66affSColin Finck 	assert(sp != NULL);
229c2c66affSColin Finck 
230c2c66affSColin Finck 	if (sp->dec_codetab == NULL) {
231c2c66affSColin Finck 		sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
232c2c66affSColin Finck 		if (sp->dec_codetab == NULL) {
233c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module,
234c2c66affSColin Finck 				     "No space for LZW code table");
235c2c66affSColin Finck 			return (0);
236c2c66affSColin Finck 		}
237c2c66affSColin Finck 		/*
238c2c66affSColin Finck 		 * Pre-load the table.
239c2c66affSColin Finck 		 */
240c2c66affSColin Finck 		code = 255;
241c2c66affSColin Finck 		do {
242c2c66affSColin Finck 			sp->dec_codetab[code].value = (unsigned char)code;
243c2c66affSColin Finck 			sp->dec_codetab[code].firstchar = (unsigned char)code;
244c2c66affSColin Finck 			sp->dec_codetab[code].length = 1;
245c2c66affSColin Finck 			sp->dec_codetab[code].next = NULL;
246c2c66affSColin Finck 		} while (code--);
247c2c66affSColin Finck 		/*
248c2c66affSColin Finck 		 * Zero-out the unused entries
249c2c66affSColin Finck                  */
250*f87faf67SThomas Faber                 /* Silence false positive */
251*f87faf67SThomas Faber                 /* coverity[overrun-buffer-arg] */
252c2c66affSColin Finck                  _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
253c2c66affSColin Finck 			     (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
254c2c66affSColin Finck 	}
255c2c66affSColin Finck 	return (1);
256c2c66affSColin Finck }
257c2c66affSColin Finck 
258c2c66affSColin Finck /*
259c2c66affSColin Finck  * Setup state for decoding a strip.
260c2c66affSColin Finck  */
261c2c66affSColin Finck static int
LZWPreDecode(TIFF * tif,uint16 s)262c2c66affSColin Finck LZWPreDecode(TIFF* tif, uint16 s)
263c2c66affSColin Finck {
264c2c66affSColin Finck 	static const char module[] = "LZWPreDecode";
265c2c66affSColin Finck 	LZWCodecState *sp = DecoderState(tif);
266c2c66affSColin Finck 
267c2c66affSColin Finck 	(void) s;
268c2c66affSColin Finck 	assert(sp != NULL);
269c2c66affSColin Finck 	if( sp->dec_codetab == NULL )
270c2c66affSColin Finck         {
271c2c66affSColin Finck             tif->tif_setupdecode( tif );
272c2c66affSColin Finck 	    if( sp->dec_codetab == NULL )
273c2c66affSColin Finck 		return (0);
274c2c66affSColin Finck         }
275c2c66affSColin Finck 
276c2c66affSColin Finck 	/*
277c2c66affSColin Finck 	 * Check for old bit-reversed codes.
278c2c66affSColin Finck 	 */
279f46b177eSThomas Faber 	if (tif->tif_rawcc >= 2 &&
280f46b177eSThomas Faber 	    tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
281c2c66affSColin Finck #ifdef LZW_COMPAT
282c2c66affSColin Finck 		if (!sp->dec_decode) {
283c2c66affSColin Finck 			TIFFWarningExt(tif->tif_clientdata, module,
284c2c66affSColin Finck 			    "Old-style LZW codes, convert file");
285c2c66affSColin Finck 			/*
286c2c66affSColin Finck 			 * Override default decoding methods with
287c2c66affSColin Finck 			 * ones that deal with the old coding.
288c2c66affSColin Finck 			 * Otherwise the predictor versions set
289c2c66affSColin Finck 			 * above will call the compatibility routines
290c2c66affSColin Finck 			 * through the dec_decode method.
291c2c66affSColin Finck 			 */
292c2c66affSColin Finck 			tif->tif_decoderow = LZWDecodeCompat;
293c2c66affSColin Finck 			tif->tif_decodestrip = LZWDecodeCompat;
294c2c66affSColin Finck 			tif->tif_decodetile = LZWDecodeCompat;
295c2c66affSColin Finck 			/*
296c2c66affSColin Finck 			 * If doing horizontal differencing, must
297c2c66affSColin Finck 			 * re-setup the predictor logic since we
298c2c66affSColin Finck 			 * switched the basic decoder methods...
299c2c66affSColin Finck 			 */
300c2c66affSColin Finck 			(*tif->tif_setupdecode)(tif);
301c2c66affSColin Finck 			sp->dec_decode = LZWDecodeCompat;
302c2c66affSColin Finck 		}
303c2c66affSColin Finck 		sp->lzw_maxcode = MAXCODE(BITS_MIN);
304c2c66affSColin Finck #else /* !LZW_COMPAT */
305c2c66affSColin Finck 		if (!sp->dec_decode) {
306c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module,
307c2c66affSColin Finck 			    "Old-style LZW codes not supported");
308c2c66affSColin Finck 			sp->dec_decode = LZWDecode;
309c2c66affSColin Finck 		}
310c2c66affSColin Finck 		return (0);
311c2c66affSColin Finck #endif/* !LZW_COMPAT */
312c2c66affSColin Finck 	} else {
313c2c66affSColin Finck 		sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
314c2c66affSColin Finck 		sp->dec_decode = LZWDecode;
315c2c66affSColin Finck 	}
316c2c66affSColin Finck 	sp->lzw_nbits = BITS_MIN;
317c2c66affSColin Finck 	sp->lzw_nextbits = 0;
318c2c66affSColin Finck 	sp->lzw_nextdata = 0;
319c2c66affSColin Finck 
320c2c66affSColin Finck 	sp->dec_restart = 0;
321c2c66affSColin Finck 	sp->dec_nbitsmask = MAXCODE(BITS_MIN);
322c2c66affSColin Finck #ifdef LZW_CHECKEOS
323c2c66affSColin Finck 	sp->dec_bitsleft = 0;
324743951ecSThomas Faber         sp->old_tif_rawcc = 0;
325c2c66affSColin Finck #endif
326c2c66affSColin Finck 	sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
327c2c66affSColin Finck 	/*
328c2c66affSColin Finck 	 * Zero entries that are not yet filled in.  We do
329c2c66affSColin Finck 	 * this to guard against bogus input data that causes
330c2c66affSColin Finck 	 * us to index into undefined entries.  If you can
331c2c66affSColin Finck 	 * come up with a way to safely bounds-check input codes
332c2c66affSColin Finck 	 * while decoding then you can remove this operation.
333c2c66affSColin Finck 	 */
334c2c66affSColin Finck 	_TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
335c2c66affSColin Finck 	sp->dec_oldcodep = &sp->dec_codetab[-1];
336c2c66affSColin Finck 	sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
337c2c66affSColin Finck 	return (1);
338c2c66affSColin Finck }
339c2c66affSColin Finck 
340c2c66affSColin Finck /*
341c2c66affSColin Finck  * Decode a "hunk of data".
342c2c66affSColin Finck  */
343c2c66affSColin Finck #define	GetNextCode(sp, bp, code) {				\
344c2c66affSColin Finck 	nextdata = (nextdata<<8) | *(bp)++;			\
345c2c66affSColin Finck 	nextbits += 8;						\
346c2c66affSColin Finck 	if (nextbits < nbits) {					\
347c2c66affSColin Finck 		nextdata = (nextdata<<8) | *(bp)++;		\
348c2c66affSColin Finck 		nextbits += 8;					\
349c2c66affSColin Finck 	}							\
350c2c66affSColin Finck 	code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask);	\
351c2c66affSColin Finck 	nextbits -= nbits;					\
352c2c66affSColin Finck }
353c2c66affSColin Finck 
354c2c66affSColin Finck static void
codeLoop(TIFF * tif,const char * module)355c2c66affSColin Finck codeLoop(TIFF* tif, const char* module)
356c2c66affSColin Finck {
357c2c66affSColin Finck 	TIFFErrorExt(tif->tif_clientdata, module,
358c2c66affSColin Finck 	    "Bogus encoding, loop in the code table; scanline %d",
359c2c66affSColin Finck 	    tif->tif_row);
360c2c66affSColin Finck }
361c2c66affSColin Finck 
362c2c66affSColin Finck static int
LZWDecode(TIFF * tif,uint8 * op0,tmsize_t occ0,uint16 s)363c2c66affSColin Finck LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
364c2c66affSColin Finck {
365c2c66affSColin Finck 	static const char module[] = "LZWDecode";
366c2c66affSColin Finck 	LZWCodecState *sp = DecoderState(tif);
367c2c66affSColin Finck 	char *op = (char*) op0;
368c2c66affSColin Finck 	long occ = (long) occ0;
369c2c66affSColin Finck 	char *tp;
370c2c66affSColin Finck 	unsigned char *bp;
371c2c66affSColin Finck 	hcode_t code;
372c2c66affSColin Finck 	int len;
373c2c66affSColin Finck 	long nbits, nextbits, nbitsmask;
374c2c66affSColin Finck         unsigned long nextdata;
375c2c66affSColin Finck 	code_t *codep, *free_entp, *maxcodep, *oldcodep;
376c2c66affSColin Finck 
377c2c66affSColin Finck 	(void) s;
378c2c66affSColin Finck 	assert(sp != NULL);
379c2c66affSColin Finck         assert(sp->dec_codetab != NULL);
380c2c66affSColin Finck 
381c2c66affSColin Finck 	/*
382c2c66affSColin Finck 	  Fail if value does not fit in long.
383c2c66affSColin Finck 	*/
384c2c66affSColin Finck 	if ((tmsize_t) occ != occ0)
385c2c66affSColin Finck 	        return (0);
386c2c66affSColin Finck 	/*
387c2c66affSColin Finck 	 * Restart interrupted output operation.
388c2c66affSColin Finck 	 */
389c2c66affSColin Finck 	if (sp->dec_restart) {
390c2c66affSColin Finck 		long residue;
391c2c66affSColin Finck 
392c2c66affSColin Finck 		codep = sp->dec_codep;
393c2c66affSColin Finck 		residue = codep->length - sp->dec_restart;
394c2c66affSColin Finck 		if (residue > occ) {
395c2c66affSColin Finck 			/*
396c2c66affSColin Finck 			 * Residue from previous decode is sufficient
397c2c66affSColin Finck 			 * to satisfy decode request.  Skip to the
398c2c66affSColin Finck 			 * start of the decoded string, place decoded
399c2c66affSColin Finck 			 * values in the output buffer, and return.
400c2c66affSColin Finck 			 */
401c2c66affSColin Finck 			sp->dec_restart += occ;
402c2c66affSColin Finck 			do {
403c2c66affSColin Finck 				codep = codep->next;
404c2c66affSColin Finck 			} while (--residue > occ && codep);
405c2c66affSColin Finck 			if (codep) {
406c2c66affSColin Finck 				tp = op + occ;
407c2c66affSColin Finck 				do {
408c2c66affSColin Finck 					*--tp = codep->value;
409c2c66affSColin Finck 					codep = codep->next;
410c2c66affSColin Finck 				} while (--occ && codep);
411c2c66affSColin Finck 			}
412c2c66affSColin Finck 			return (1);
413c2c66affSColin Finck 		}
414c2c66affSColin Finck 		/*
415c2c66affSColin Finck 		 * Residue satisfies only part of the decode request.
416c2c66affSColin Finck 		 */
417c2c66affSColin Finck 		op += residue;
418c2c66affSColin Finck 		occ -= residue;
419c2c66affSColin Finck 		tp = op;
420c2c66affSColin Finck 		do {
421c2c66affSColin Finck 			int t;
422c2c66affSColin Finck 			--tp;
423c2c66affSColin Finck 			t = codep->value;
424c2c66affSColin Finck 			codep = codep->next;
425c2c66affSColin Finck 			*tp = (char)t;
426c2c66affSColin Finck 		} while (--residue && codep);
427c2c66affSColin Finck 		sp->dec_restart = 0;
428c2c66affSColin Finck 	}
429c2c66affSColin Finck 
430c2c66affSColin Finck 	bp = (unsigned char *)tif->tif_rawcp;
431c2c66affSColin Finck #ifdef LZW_CHECKEOS
432743951ecSThomas Faber 	sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
433c2c66affSColin Finck #endif
434c2c66affSColin Finck 	nbits = sp->lzw_nbits;
435c2c66affSColin Finck 	nextdata = sp->lzw_nextdata;
436c2c66affSColin Finck 	nextbits = sp->lzw_nextbits;
437c2c66affSColin Finck 	nbitsmask = sp->dec_nbitsmask;
438c2c66affSColin Finck 	oldcodep = sp->dec_oldcodep;
439c2c66affSColin Finck 	free_entp = sp->dec_free_entp;
440c2c66affSColin Finck 	maxcodep = sp->dec_maxcodep;
441c2c66affSColin Finck 
442c2c66affSColin Finck 	while (occ > 0) {
443c2c66affSColin Finck 		NextCode(tif, sp, bp, code, GetNextCode);
444c2c66affSColin Finck 		if (code == CODE_EOI)
445c2c66affSColin Finck 			break;
446c2c66affSColin Finck 		if (code == CODE_CLEAR) {
447c2c66affSColin Finck 			do {
448c2c66affSColin Finck 				free_entp = sp->dec_codetab + CODE_FIRST;
449c2c66affSColin Finck 				_TIFFmemset(free_entp, 0,
450c2c66affSColin Finck 					    (CSIZE - CODE_FIRST) * sizeof (code_t));
451c2c66affSColin Finck 				nbits = BITS_MIN;
452c2c66affSColin Finck 				nbitsmask = MAXCODE(BITS_MIN);
453c2c66affSColin Finck 				maxcodep = sp->dec_codetab + nbitsmask-1;
454c2c66affSColin Finck 				NextCode(tif, sp, bp, code, GetNextCode);
455c2c66affSColin Finck 			} while (code == CODE_CLEAR);	/* consecutive CODE_CLEAR codes */
456c2c66affSColin Finck 			if (code == CODE_EOI)
457c2c66affSColin Finck 				break;
458c2c66affSColin Finck 			if (code > CODE_CLEAR) {
459c2c66affSColin Finck 				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
460c2c66affSColin Finck 				"LZWDecode: Corrupted LZW table at scanline %d",
461c2c66affSColin Finck 					     tif->tif_row);
462c2c66affSColin Finck 				return (0);
463c2c66affSColin Finck 			}
464c2c66affSColin Finck 			*op++ = (char)code;
465c2c66affSColin Finck 			occ--;
466c2c66affSColin Finck 			oldcodep = sp->dec_codetab + code;
467c2c66affSColin Finck 			continue;
468c2c66affSColin Finck 		}
469c2c66affSColin Finck 		codep = sp->dec_codetab + code;
470c2c66affSColin Finck 
471c2c66affSColin Finck 		/*
472c2c66affSColin Finck 		 * Add the new entry to the code table.
473c2c66affSColin Finck 		 */
474c2c66affSColin Finck 		if (free_entp < &sp->dec_codetab[0] ||
475c2c66affSColin Finck 		    free_entp >= &sp->dec_codetab[CSIZE]) {
476c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module,
477c2c66affSColin Finck 			    "Corrupted LZW table at scanline %d",
478c2c66affSColin Finck 			    tif->tif_row);
479c2c66affSColin Finck 			return (0);
480c2c66affSColin Finck 		}
481c2c66affSColin Finck 
482c2c66affSColin Finck 		free_entp->next = oldcodep;
483c2c66affSColin Finck 		if (free_entp->next < &sp->dec_codetab[0] ||
484c2c66affSColin Finck 		    free_entp->next >= &sp->dec_codetab[CSIZE]) {
485c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module,
486c2c66affSColin Finck 			    "Corrupted LZW table at scanline %d",
487c2c66affSColin Finck 			    tif->tif_row);
488c2c66affSColin Finck 			return (0);
489c2c66affSColin Finck 		}
490c2c66affSColin Finck 		free_entp->firstchar = free_entp->next->firstchar;
491c2c66affSColin Finck 		free_entp->length = free_entp->next->length+1;
492c2c66affSColin Finck 		free_entp->value = (codep < free_entp) ?
493c2c66affSColin Finck 		    codep->firstchar : free_entp->firstchar;
494c2c66affSColin Finck 		if (++free_entp > maxcodep) {
495c2c66affSColin Finck 			if (++nbits > BITS_MAX)		/* should not happen */
496c2c66affSColin Finck 				nbits = BITS_MAX;
497c2c66affSColin Finck 			nbitsmask = MAXCODE(nbits);
498c2c66affSColin Finck 			maxcodep = sp->dec_codetab + nbitsmask-1;
499c2c66affSColin Finck 		}
500c2c66affSColin Finck 		oldcodep = codep;
501c2c66affSColin Finck 		if (code >= 256) {
502c2c66affSColin Finck 			/*
503c2c66affSColin Finck 			 * Code maps to a string, copy string
504c2c66affSColin Finck 			 * value to output (written in reverse).
505c2c66affSColin Finck 			 */
506c2c66affSColin Finck 			if(codep->length == 0) {
507c2c66affSColin Finck 				TIFFErrorExt(tif->tif_clientdata, module,
508c2c66affSColin Finck 				    "Wrong length of decoded string: "
509c2c66affSColin Finck 				    "data probably corrupted at scanline %d",
510c2c66affSColin Finck 				    tif->tif_row);
511c2c66affSColin Finck 				return (0);
512c2c66affSColin Finck 			}
513c2c66affSColin Finck 			if (codep->length > occ) {
514c2c66affSColin Finck 				/*
515c2c66affSColin Finck 				 * String is too long for decode buffer,
516c2c66affSColin Finck 				 * locate portion that will fit, copy to
517c2c66affSColin Finck 				 * the decode buffer, and setup restart
518c2c66affSColin Finck 				 * logic for the next decoding call.
519c2c66affSColin Finck 				 */
520c2c66affSColin Finck 				sp->dec_codep = codep;
521c2c66affSColin Finck 				do {
522c2c66affSColin Finck 					codep = codep->next;
523c2c66affSColin Finck 				} while (codep && codep->length > occ);
524c2c66affSColin Finck 				if (codep) {
525c2c66affSColin Finck 					sp->dec_restart = (long)occ;
526c2c66affSColin Finck 					tp = op + occ;
527c2c66affSColin Finck 					do  {
528c2c66affSColin Finck 						*--tp = codep->value;
529c2c66affSColin Finck 						codep = codep->next;
530c2c66affSColin Finck 					}  while (--occ && codep);
531c2c66affSColin Finck 					if (codep)
532c2c66affSColin Finck 						codeLoop(tif, module);
533c2c66affSColin Finck 				}
534c2c66affSColin Finck 				break;
535c2c66affSColin Finck 			}
536c2c66affSColin Finck 			len = codep->length;
537c2c66affSColin Finck 			tp = op + len;
538c2c66affSColin Finck 			do {
539c2c66affSColin Finck 				int t;
540c2c66affSColin Finck 				--tp;
541c2c66affSColin Finck 				t = codep->value;
542c2c66affSColin Finck 				codep = codep->next;
543c2c66affSColin Finck 				*tp = (char)t;
544c2c66affSColin Finck 			} while (codep && tp > op);
545c2c66affSColin Finck 			if (codep) {
546c2c66affSColin Finck 			    codeLoop(tif, module);
547c2c66affSColin Finck 			    break;
548c2c66affSColin Finck 			}
549c2c66affSColin Finck 			assert(occ >= len);
550c2c66affSColin Finck 			op += len;
551c2c66affSColin Finck 			occ -= len;
552c2c66affSColin Finck 		} else {
553c2c66affSColin Finck 			*op++ = (char)code;
554c2c66affSColin Finck 			occ--;
555c2c66affSColin Finck 		}
556c2c66affSColin Finck 	}
557c2c66affSColin Finck 
558c2c66affSColin Finck 	tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
559c2c66affSColin Finck 	tif->tif_rawcp = (uint8*) bp;
560743951ecSThomas Faber #ifdef LZW_CHECKEOS
561743951ecSThomas Faber 	sp->old_tif_rawcc = tif->tif_rawcc;
562743951ecSThomas Faber #endif
563c2c66affSColin Finck 	sp->lzw_nbits = (unsigned short) nbits;
564c2c66affSColin Finck 	sp->lzw_nextdata = nextdata;
565c2c66affSColin Finck 	sp->lzw_nextbits = nextbits;
566c2c66affSColin Finck 	sp->dec_nbitsmask = nbitsmask;
567c2c66affSColin Finck 	sp->dec_oldcodep = oldcodep;
568c2c66affSColin Finck 	sp->dec_free_entp = free_entp;
569c2c66affSColin Finck 	sp->dec_maxcodep = maxcodep;
570c2c66affSColin Finck 
571c2c66affSColin Finck 	if (occ > 0) {
572c2c66affSColin Finck #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
573c2c66affSColin Finck 		TIFFErrorExt(tif->tif_clientdata, module,
574c2c66affSColin Finck 			"Not enough data at scanline %d (short %I64d bytes)",
575c2c66affSColin Finck 			     tif->tif_row, (unsigned __int64) occ);
576c2c66affSColin Finck #else
577c2c66affSColin Finck 		TIFFErrorExt(tif->tif_clientdata, module,
578c2c66affSColin Finck 			"Not enough data at scanline %d (short %llu bytes)",
579c2c66affSColin Finck 			     tif->tif_row, (unsigned long long) occ);
580c2c66affSColin Finck #endif
581c2c66affSColin Finck 		return (0);
582c2c66affSColin Finck 	}
583c2c66affSColin Finck 	return (1);
584c2c66affSColin Finck }
585c2c66affSColin Finck 
586c2c66affSColin Finck #ifdef LZW_COMPAT
587c2c66affSColin Finck /*
588c2c66affSColin Finck  * Decode a "hunk of data" for old images.
589c2c66affSColin Finck  */
590c2c66affSColin Finck #define	GetNextCodeCompat(sp, bp, code) {			\
591c2c66affSColin Finck 	nextdata |= (unsigned long) *(bp)++ << nextbits;	\
592c2c66affSColin Finck 	nextbits += 8;						\
593c2c66affSColin Finck 	if (nextbits < nbits) {					\
594c2c66affSColin Finck 		nextdata |= (unsigned long) *(bp)++ << nextbits;\
595c2c66affSColin Finck 		nextbits += 8;					\
596c2c66affSColin Finck 	}							\
597c2c66affSColin Finck 	code = (hcode_t)(nextdata & nbitsmask);			\
598c2c66affSColin Finck 	nextdata >>= nbits;					\
599c2c66affSColin Finck 	nextbits -= nbits;					\
600c2c66affSColin Finck }
601c2c66affSColin Finck 
602c2c66affSColin Finck static int
LZWDecodeCompat(TIFF * tif,uint8 * op0,tmsize_t occ0,uint16 s)603c2c66affSColin Finck LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
604c2c66affSColin Finck {
605c2c66affSColin Finck 	static const char module[] = "LZWDecodeCompat";
606c2c66affSColin Finck 	LZWCodecState *sp = DecoderState(tif);
607c2c66affSColin Finck 	char *op = (char*) op0;
608c2c66affSColin Finck 	long occ = (long) occ0;
609c2c66affSColin Finck 	char *tp;
610c2c66affSColin Finck 	unsigned char *bp;
611c2c66affSColin Finck 	int code, nbits;
612743951ecSThomas Faber 	int len;
613c2c66affSColin Finck 	long nextbits, nextdata, nbitsmask;
614c2c66affSColin Finck 	code_t *codep, *free_entp, *maxcodep, *oldcodep;
615c2c66affSColin Finck 
616c2c66affSColin Finck 	(void) s;
617c2c66affSColin Finck 	assert(sp != NULL);
618c2c66affSColin Finck 
619c2c66affSColin Finck 	/*
620c2c66affSColin Finck 	  Fail if value does not fit in long.
621c2c66affSColin Finck 	*/
622c2c66affSColin Finck 	if ((tmsize_t) occ != occ0)
623c2c66affSColin Finck 	        return (0);
624c2c66affSColin Finck 
625c2c66affSColin Finck 	/*
626c2c66affSColin Finck 	 * Restart interrupted output operation.
627c2c66affSColin Finck 	 */
628c2c66affSColin Finck 	if (sp->dec_restart) {
629c2c66affSColin Finck 		long residue;
630c2c66affSColin Finck 
631c2c66affSColin Finck 		codep = sp->dec_codep;
632c2c66affSColin Finck 		residue = codep->length - sp->dec_restart;
633c2c66affSColin Finck 		if (residue > occ) {
634c2c66affSColin Finck 			/*
635c2c66affSColin Finck 			 * Residue from previous decode is sufficient
636c2c66affSColin Finck 			 * to satisfy decode request.  Skip to the
637c2c66affSColin Finck 			 * start of the decoded string, place decoded
638c2c66affSColin Finck 			 * values in the output buffer, and return.
639c2c66affSColin Finck 			 */
640c2c66affSColin Finck 			sp->dec_restart += occ;
641c2c66affSColin Finck 			do {
642c2c66affSColin Finck 				codep = codep->next;
643c2c66affSColin Finck 			} while (--residue > occ);
644c2c66affSColin Finck 			tp = op + occ;
645c2c66affSColin Finck 			do {
646c2c66affSColin Finck 				*--tp = codep->value;
647c2c66affSColin Finck 				codep = codep->next;
648c2c66affSColin Finck 			} while (--occ);
649c2c66affSColin Finck 			return (1);
650c2c66affSColin Finck 		}
651c2c66affSColin Finck 		/*
652c2c66affSColin Finck 		 * Residue satisfies only part of the decode request.
653c2c66affSColin Finck 		 */
654c2c66affSColin Finck 		op += residue;
655c2c66affSColin Finck 		occ -= residue;
656c2c66affSColin Finck 		tp = op;
657c2c66affSColin Finck 		do {
658c2c66affSColin Finck 			*--tp = codep->value;
659c2c66affSColin Finck 			codep = codep->next;
660c2c66affSColin Finck 		} while (--residue);
661c2c66affSColin Finck 		sp->dec_restart = 0;
662c2c66affSColin Finck 	}
663c2c66affSColin Finck 
664c2c66affSColin Finck 	bp = (unsigned char *)tif->tif_rawcp;
665f46b177eSThomas Faber #ifdef LZW_CHECKEOS
666743951ecSThomas Faber 	sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
667f46b177eSThomas Faber #endif
668c2c66affSColin Finck 	nbits = sp->lzw_nbits;
669c2c66affSColin Finck 	nextdata = sp->lzw_nextdata;
670c2c66affSColin Finck 	nextbits = sp->lzw_nextbits;
671c2c66affSColin Finck 	nbitsmask = sp->dec_nbitsmask;
672c2c66affSColin Finck 	oldcodep = sp->dec_oldcodep;
673c2c66affSColin Finck 	free_entp = sp->dec_free_entp;
674c2c66affSColin Finck 	maxcodep = sp->dec_maxcodep;
675c2c66affSColin Finck 
676c2c66affSColin Finck 	while (occ > 0) {
677c2c66affSColin Finck 		NextCode(tif, sp, bp, code, GetNextCodeCompat);
678c2c66affSColin Finck 		if (code == CODE_EOI)
679c2c66affSColin Finck 			break;
680c2c66affSColin Finck 		if (code == CODE_CLEAR) {
681c2c66affSColin Finck 			do {
682c2c66affSColin Finck 				free_entp = sp->dec_codetab + CODE_FIRST;
683c2c66affSColin Finck 				_TIFFmemset(free_entp, 0,
684c2c66affSColin Finck 					    (CSIZE - CODE_FIRST) * sizeof (code_t));
685c2c66affSColin Finck 				nbits = BITS_MIN;
686c2c66affSColin Finck 				nbitsmask = MAXCODE(BITS_MIN);
687c2c66affSColin Finck 				maxcodep = sp->dec_codetab + nbitsmask;
688c2c66affSColin Finck 				NextCode(tif, sp, bp, code, GetNextCodeCompat);
689c2c66affSColin Finck 			} while (code == CODE_CLEAR);	/* consecutive CODE_CLEAR codes */
690c2c66affSColin Finck 			if (code == CODE_EOI)
691c2c66affSColin Finck 				break;
692c2c66affSColin Finck 			if (code > CODE_CLEAR) {
693c2c66affSColin Finck 				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
694c2c66affSColin Finck 				"LZWDecode: Corrupted LZW table at scanline %d",
695c2c66affSColin Finck 					     tif->tif_row);
696c2c66affSColin Finck 				return (0);
697c2c66affSColin Finck 			}
698c2c66affSColin Finck 			*op++ = (char)code;
699c2c66affSColin Finck 			occ--;
700c2c66affSColin Finck 			oldcodep = sp->dec_codetab + code;
701c2c66affSColin Finck 			continue;
702c2c66affSColin Finck 		}
703c2c66affSColin Finck 		codep = sp->dec_codetab + code;
704c2c66affSColin Finck 
705c2c66affSColin Finck 		/*
706c2c66affSColin Finck 		 * Add the new entry to the code table.
707c2c66affSColin Finck 		 */
708c2c66affSColin Finck 		if (free_entp < &sp->dec_codetab[0] ||
709c2c66affSColin Finck 		    free_entp >= &sp->dec_codetab[CSIZE]) {
710c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module,
711c2c66affSColin Finck 			    "Corrupted LZW table at scanline %d", tif->tif_row);
712c2c66affSColin Finck 			return (0);
713c2c66affSColin Finck 		}
714c2c66affSColin Finck 
715c2c66affSColin Finck 		free_entp->next = oldcodep;
716c2c66affSColin Finck 		if (free_entp->next < &sp->dec_codetab[0] ||
717c2c66affSColin Finck 		    free_entp->next >= &sp->dec_codetab[CSIZE]) {
718c2c66affSColin Finck 			TIFFErrorExt(tif->tif_clientdata, module,
719c2c66affSColin Finck 			    "Corrupted LZW table at scanline %d", tif->tif_row);
720c2c66affSColin Finck 			return (0);
721c2c66affSColin Finck 		}
722c2c66affSColin Finck 		free_entp->firstchar = free_entp->next->firstchar;
723c2c66affSColin Finck 		free_entp->length = free_entp->next->length+1;
724c2c66affSColin Finck 		free_entp->value = (codep < free_entp) ?
725c2c66affSColin Finck 		    codep->firstchar : free_entp->firstchar;
726c2c66affSColin Finck 		if (++free_entp > maxcodep) {
727c2c66affSColin Finck 			if (++nbits > BITS_MAX)		/* should not happen */
728c2c66affSColin Finck 				nbits = BITS_MAX;
729c2c66affSColin Finck 			nbitsmask = MAXCODE(nbits);
730c2c66affSColin Finck 			maxcodep = sp->dec_codetab + nbitsmask;
731c2c66affSColin Finck 		}
732c2c66affSColin Finck 		oldcodep = codep;
733c2c66affSColin Finck 		if (code >= 256) {
734c2c66affSColin Finck 			/*
735c2c66affSColin Finck 			 * Code maps to a string, copy string
736c2c66affSColin Finck 			 * value to output (written in reverse).
737c2c66affSColin Finck 			 */
738c2c66affSColin Finck 			if(codep->length == 0) {
739c2c66affSColin Finck 				TIFFErrorExt(tif->tif_clientdata, module,
740c2c66affSColin Finck 				    "Wrong length of decoded "
741c2c66affSColin Finck 				    "string: data probably corrupted at scanline %d",
742c2c66affSColin Finck 				    tif->tif_row);
743c2c66affSColin Finck 				return (0);
744c2c66affSColin Finck 			}
745c2c66affSColin Finck 			if (codep->length > occ) {
746c2c66affSColin Finck 				/*
747c2c66affSColin Finck 				 * String is too long for decode buffer,
748c2c66affSColin Finck 				 * locate portion that will fit, copy to
749c2c66affSColin Finck 				 * the decode buffer, and setup restart
750c2c66affSColin Finck 				 * logic for the next decoding call.
751c2c66affSColin Finck 				 */
752c2c66affSColin Finck 				sp->dec_codep = codep;
753c2c66affSColin Finck 				do {
754c2c66affSColin Finck 					codep = codep->next;
755c2c66affSColin Finck 				} while (codep->length > occ);
756c2c66affSColin Finck 				sp->dec_restart = occ;
757c2c66affSColin Finck 				tp = op + occ;
758c2c66affSColin Finck 				do  {
759c2c66affSColin Finck 					*--tp = codep->value;
760c2c66affSColin Finck 					codep = codep->next;
761c2c66affSColin Finck 				}  while (--occ);
762c2c66affSColin Finck 				break;
763c2c66affSColin Finck 			}
764743951ecSThomas Faber 			len = codep->length;
765743951ecSThomas Faber 			tp = op + len;
766c2c66affSColin Finck 			do {
767743951ecSThomas Faber 				int t;
768743951ecSThomas Faber 				--tp;
769743951ecSThomas Faber 				t = codep->value;
770743951ecSThomas Faber 				codep = codep->next;
771743951ecSThomas Faber 				*tp = (char)t;
772743951ecSThomas Faber 			} while (codep && tp > op);
773743951ecSThomas Faber 			assert(occ >= len);
774743951ecSThomas Faber 			op += len;
775743951ecSThomas Faber 			occ -= len;
776c2c66affSColin Finck 		} else {
777c2c66affSColin Finck 			*op++ = (char)code;
778c2c66affSColin Finck 			occ--;
779c2c66affSColin Finck 		}
780c2c66affSColin Finck 	}
781c2c66affSColin Finck 
782f46b177eSThomas Faber 	tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
783c2c66affSColin Finck 	tif->tif_rawcp = (uint8*) bp;
784743951ecSThomas Faber #ifdef LZW_CHECKEOS
785743951ecSThomas Faber 	sp->old_tif_rawcc = tif->tif_rawcc;
786743951ecSThomas Faber #endif
787c2c66affSColin Finck 	sp->lzw_nbits = (unsigned short)nbits;
788c2c66affSColin Finck 	sp->lzw_nextdata = nextdata;
789c2c66affSColin Finck 	sp->lzw_nextbits = nextbits;
790c2c66affSColin Finck 	sp->dec_nbitsmask = nbitsmask;
791c2c66affSColin Finck 	sp->dec_oldcodep = oldcodep;
792c2c66affSColin Finck 	sp->dec_free_entp = free_entp;
793c2c66affSColin Finck 	sp->dec_maxcodep = maxcodep;
794c2c66affSColin Finck 
795c2c66affSColin Finck 	if (occ > 0) {
796c2c66affSColin Finck #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
797c2c66affSColin Finck 		TIFFErrorExt(tif->tif_clientdata, module,
798c2c66affSColin Finck 			"Not enough data at scanline %d (short %I64d bytes)",
799c2c66affSColin Finck 			     tif->tif_row, (unsigned __int64) occ);
800c2c66affSColin Finck #else
801c2c66affSColin Finck 		TIFFErrorExt(tif->tif_clientdata, module,
802c2c66affSColin Finck 			"Not enough data at scanline %d (short %llu bytes)",
803c2c66affSColin Finck 			     tif->tif_row, (unsigned long long) occ);
804c2c66affSColin Finck #endif
805c2c66affSColin Finck 		return (0);
806c2c66affSColin Finck 	}
807c2c66affSColin Finck 	return (1);
808c2c66affSColin Finck }
809c2c66affSColin Finck #endif /* LZW_COMPAT */
810c2c66affSColin Finck 
811c2c66affSColin Finck /*
812c2c66affSColin Finck  * LZW Encoding.
813c2c66affSColin Finck  */
814c2c66affSColin Finck 
815c2c66affSColin Finck static int
LZWSetupEncode(TIFF * tif)816c2c66affSColin Finck LZWSetupEncode(TIFF* tif)
817c2c66affSColin Finck {
818c2c66affSColin Finck 	static const char module[] = "LZWSetupEncode";
819c2c66affSColin Finck 	LZWCodecState* sp = EncoderState(tif);
820c2c66affSColin Finck 
821c2c66affSColin Finck 	assert(sp != NULL);
822c2c66affSColin Finck 	sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
823c2c66affSColin Finck 	if (sp->enc_hashtab == NULL) {
824c2c66affSColin Finck 		TIFFErrorExt(tif->tif_clientdata, module,
825c2c66affSColin Finck 			     "No space for LZW hash table");
826c2c66affSColin Finck 		return (0);
827c2c66affSColin Finck 	}
828c2c66affSColin Finck 	return (1);
829c2c66affSColin Finck }
830c2c66affSColin Finck 
831c2c66affSColin Finck /*
832c2c66affSColin Finck  * Reset encoding state at the start of a strip.
833c2c66affSColin Finck  */
834c2c66affSColin Finck static int
LZWPreEncode(TIFF * tif,uint16 s)835c2c66affSColin Finck LZWPreEncode(TIFF* tif, uint16 s)
836c2c66affSColin Finck {
837c2c66affSColin Finck 	LZWCodecState *sp = EncoderState(tif);
838c2c66affSColin Finck 
839c2c66affSColin Finck 	(void) s;
840c2c66affSColin Finck 	assert(sp != NULL);
841c2c66affSColin Finck 
842c2c66affSColin Finck 	if( sp->enc_hashtab == NULL )
843c2c66affSColin Finck         {
844c2c66affSColin Finck             tif->tif_setupencode( tif );
845c2c66affSColin Finck         }
846c2c66affSColin Finck 
847c2c66affSColin Finck 	sp->lzw_nbits = BITS_MIN;
848c2c66affSColin Finck 	sp->lzw_maxcode = MAXCODE(BITS_MIN);
849c2c66affSColin Finck 	sp->lzw_free_ent = CODE_FIRST;
850c2c66affSColin Finck 	sp->lzw_nextbits = 0;
851c2c66affSColin Finck 	sp->lzw_nextdata = 0;
852c2c66affSColin Finck 	sp->enc_checkpoint = CHECK_GAP;
853c2c66affSColin Finck 	sp->enc_ratio = 0;
854c2c66affSColin Finck 	sp->enc_incount = 0;
855c2c66affSColin Finck 	sp->enc_outcount = 0;
856c2c66affSColin Finck 	/*
857c2c66affSColin Finck 	 * The 4 here insures there is space for 2 max-sized
858c2c66affSColin Finck 	 * codes in LZWEncode and LZWPostDecode.
859c2c66affSColin Finck 	 */
860c2c66affSColin Finck 	sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
861c2c66affSColin Finck 	cl_hash(sp);		/* clear hash table */
862c2c66affSColin Finck 	sp->enc_oldcode = (hcode_t) -1;	/* generates CODE_CLEAR in LZWEncode */
863c2c66affSColin Finck 	return (1);
864c2c66affSColin Finck }
865c2c66affSColin Finck 
866c2c66affSColin Finck #define	CALCRATIO(sp, rat) {					\
867c2c66affSColin Finck 	if (incount > 0x007fffff) { /* NB: shift will overflow */\
868c2c66affSColin Finck 		rat = outcount >> 8;				\
869c2c66affSColin Finck 		rat = (rat == 0 ? 0x7fffffff : incount/rat);	\
870c2c66affSColin Finck 	} else							\
871c2c66affSColin Finck 		rat = (incount<<8) / outcount;			\
872c2c66affSColin Finck }
873c2c66affSColin Finck 
874c2c66affSColin Finck /* Explicit 0xff masking to make icc -check=conversions happy */
875c2c66affSColin Finck #define	PutNextCode(op, c) {					\
876c2c66affSColin Finck 	nextdata = (nextdata << nbits) | c;			\
877c2c66affSColin Finck 	nextbits += nbits;					\
878c2c66affSColin Finck 	*op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff);		\
879c2c66affSColin Finck 	nextbits -= 8;						\
880c2c66affSColin Finck 	if (nextbits >= 8) {					\
881c2c66affSColin Finck 		*op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff);	\
882c2c66affSColin Finck 		nextbits -= 8;					\
883c2c66affSColin Finck 	}							\
884c2c66affSColin Finck 	outcount += nbits;					\
885c2c66affSColin Finck }
886c2c66affSColin Finck 
887c2c66affSColin Finck /*
888c2c66affSColin Finck  * Encode a chunk of pixels.
889c2c66affSColin Finck  *
890c2c66affSColin Finck  * Uses an open addressing double hashing (no chaining) on the
891c2c66affSColin Finck  * prefix code/next character combination.  We do a variant of
892c2c66affSColin Finck  * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
893c2c66affSColin Finck  * relatively-prime secondary probe.  Here, the modular division
894c2c66affSColin Finck  * first probe is gives way to a faster exclusive-or manipulation.
895c2c66affSColin Finck  * Also do block compression with an adaptive reset, whereby the
896c2c66affSColin Finck  * code table is cleared when the compression ratio decreases,
897c2c66affSColin Finck  * but after the table fills.  The variable-length output codes
898c2c66affSColin Finck  * are re-sized at this point, and a CODE_CLEAR is generated
899c2c66affSColin Finck  * for the decoder.
900c2c66affSColin Finck  */
901c2c66affSColin Finck static int
LZWEncode(TIFF * tif,uint8 * bp,tmsize_t cc,uint16 s)902c2c66affSColin Finck LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
903c2c66affSColin Finck {
904c2c66affSColin Finck 	register LZWCodecState *sp = EncoderState(tif);
905c2c66affSColin Finck 	register long fcode;
906c2c66affSColin Finck 	register hash_t *hp;
907c2c66affSColin Finck 	register int h, c;
908c2c66affSColin Finck 	hcode_t ent;
909c2c66affSColin Finck 	long disp;
910c2c66affSColin Finck 	long incount, outcount, checkpoint;
911c2c66affSColin Finck 	unsigned long nextdata;
912c2c66affSColin Finck         long nextbits;
913c2c66affSColin Finck 	int free_ent, maxcode, nbits;
914c2c66affSColin Finck 	uint8* op;
915c2c66affSColin Finck 	uint8* limit;
916c2c66affSColin Finck 
917c2c66affSColin Finck 	(void) s;
918c2c66affSColin Finck 	if (sp == NULL)
919c2c66affSColin Finck 		return (0);
920c2c66affSColin Finck 
921c2c66affSColin Finck         assert(sp->enc_hashtab != NULL);
922c2c66affSColin Finck 
923c2c66affSColin Finck 	/*
924c2c66affSColin Finck 	 * Load local state.
925c2c66affSColin Finck 	 */
926c2c66affSColin Finck 	incount = sp->enc_incount;
927c2c66affSColin Finck 	outcount = sp->enc_outcount;
928c2c66affSColin Finck 	checkpoint = sp->enc_checkpoint;
929c2c66affSColin Finck 	nextdata = sp->lzw_nextdata;
930c2c66affSColin Finck 	nextbits = sp->lzw_nextbits;
931c2c66affSColin Finck 	free_ent = sp->lzw_free_ent;
932c2c66affSColin Finck 	maxcode = sp->lzw_maxcode;
933c2c66affSColin Finck 	nbits = sp->lzw_nbits;
934c2c66affSColin Finck 	op = tif->tif_rawcp;
935c2c66affSColin Finck 	limit = sp->enc_rawlimit;
936c2c66affSColin Finck 	ent = (hcode_t)sp->enc_oldcode;
937c2c66affSColin Finck 
938c2c66affSColin Finck 	if (ent == (hcode_t) -1 && cc > 0) {
939c2c66affSColin Finck 		/*
940c2c66affSColin Finck 		 * NB: This is safe because it can only happen
941c2c66affSColin Finck 		 *     at the start of a strip where we know there
942c2c66affSColin Finck 		 *     is space in the data buffer.
943c2c66affSColin Finck 		 */
944c2c66affSColin Finck 		PutNextCode(op, CODE_CLEAR);
945c2c66affSColin Finck 		ent = *bp++; cc--; incount++;
946c2c66affSColin Finck 	}
947c2c66affSColin Finck 	while (cc > 0) {
948c2c66affSColin Finck 		c = *bp++; cc--; incount++;
949c2c66affSColin Finck 		fcode = ((long)c << BITS_MAX) + ent;
950c2c66affSColin Finck 		h = (c << HSHIFT) ^ ent;	/* xor hashing */
951c2c66affSColin Finck #ifdef _WINDOWS
952c2c66affSColin Finck 		/*
953c2c66affSColin Finck 		 * Check hash index for an overflow.
954c2c66affSColin Finck 		 */
955c2c66affSColin Finck 		if (h >= HSIZE)
956c2c66affSColin Finck 			h -= HSIZE;
957c2c66affSColin Finck #endif
958c2c66affSColin Finck 		hp = &sp->enc_hashtab[h];
959c2c66affSColin Finck 		if (hp->hash == fcode) {
960c2c66affSColin Finck 			ent = hp->code;
961c2c66affSColin Finck 			continue;
962c2c66affSColin Finck 		}
963c2c66affSColin Finck 		if (hp->hash >= 0) {
964c2c66affSColin Finck 			/*
965c2c66affSColin Finck 			 * Primary hash failed, check secondary hash.
966c2c66affSColin Finck 			 */
967c2c66affSColin Finck 			disp = HSIZE - h;
968c2c66affSColin Finck 			if (h == 0)
969c2c66affSColin Finck 				disp = 1;
970c2c66affSColin Finck 			do {
971c2c66affSColin Finck 				/*
972c2c66affSColin Finck 				 * Avoid pointer arithmetic because of
973c2c66affSColin Finck 				 * wraparound problems with segments.
974c2c66affSColin Finck 				 */
975c2c66affSColin Finck 				if ((h -= disp) < 0)
976c2c66affSColin Finck 					h += HSIZE;
977c2c66affSColin Finck 				hp = &sp->enc_hashtab[h];
978c2c66affSColin Finck 				if (hp->hash == fcode) {
979c2c66affSColin Finck 					ent = hp->code;
980c2c66affSColin Finck 					goto hit;
981c2c66affSColin Finck 				}
982c2c66affSColin Finck 			} while (hp->hash >= 0);
983c2c66affSColin Finck 		}
984c2c66affSColin Finck 		/*
985c2c66affSColin Finck 		 * New entry, emit code and add to table.
986c2c66affSColin Finck 		 */
987c2c66affSColin Finck 		/*
988c2c66affSColin Finck 		 * Verify there is space in the buffer for the code
989c2c66affSColin Finck 		 * and any potential Clear code that might be emitted
990c2c66affSColin Finck 		 * below.  The value of limit is setup so that there
991c2c66affSColin Finck 		 * are at least 4 bytes free--room for 2 codes.
992c2c66affSColin Finck 		 */
993c2c66affSColin Finck 		if (op > limit) {
994c2c66affSColin Finck 			tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
995c2c66affSColin Finck 			if( !TIFFFlushData1(tif) )
996c2c66affSColin Finck                             return 0;
997c2c66affSColin Finck 			op = tif->tif_rawdata;
998c2c66affSColin Finck 		}
999c2c66affSColin Finck 		PutNextCode(op, ent);
1000c2c66affSColin Finck 		ent = (hcode_t)c;
1001c2c66affSColin Finck 		hp->code = (hcode_t)(free_ent++);
1002c2c66affSColin Finck 		hp->hash = fcode;
1003c2c66affSColin Finck 		if (free_ent == CODE_MAX-1) {
1004c2c66affSColin Finck 			/* table is full, emit clear code and reset */
1005c2c66affSColin Finck 			cl_hash(sp);
1006c2c66affSColin Finck 			sp->enc_ratio = 0;
1007c2c66affSColin Finck 			incount = 0;
1008c2c66affSColin Finck 			outcount = 0;
1009c2c66affSColin Finck 			free_ent = CODE_FIRST;
1010c2c66affSColin Finck 			PutNextCode(op, CODE_CLEAR);
1011c2c66affSColin Finck 			nbits = BITS_MIN;
1012c2c66affSColin Finck 			maxcode = MAXCODE(BITS_MIN);
1013c2c66affSColin Finck 		} else {
1014c2c66affSColin Finck 			/*
1015c2c66affSColin Finck 			 * If the next entry is going to be too big for
1016c2c66affSColin Finck 			 * the code size, then increase it, if possible.
1017c2c66affSColin Finck 			 */
1018c2c66affSColin Finck 			if (free_ent > maxcode) {
1019c2c66affSColin Finck 				nbits++;
1020c2c66affSColin Finck 				assert(nbits <= BITS_MAX);
1021c2c66affSColin Finck 				maxcode = (int) MAXCODE(nbits);
1022c2c66affSColin Finck 			} else if (incount >= checkpoint) {
1023c2c66affSColin Finck 				long rat;
1024c2c66affSColin Finck 				/*
1025c2c66affSColin Finck 				 * Check compression ratio and, if things seem
1026c2c66affSColin Finck 				 * to be slipping, clear the hash table and
1027c2c66affSColin Finck 				 * reset state.  The compression ratio is a
1028c2c66affSColin Finck 				 * 24+8-bit fractional number.
1029c2c66affSColin Finck 				 */
1030c2c66affSColin Finck 				checkpoint = incount+CHECK_GAP;
1031c2c66affSColin Finck 				CALCRATIO(sp, rat);
1032c2c66affSColin Finck 				if (rat <= sp->enc_ratio) {
1033c2c66affSColin Finck 					cl_hash(sp);
1034c2c66affSColin Finck 					sp->enc_ratio = 0;
1035c2c66affSColin Finck 					incount = 0;
1036c2c66affSColin Finck 					outcount = 0;
1037c2c66affSColin Finck 					free_ent = CODE_FIRST;
1038c2c66affSColin Finck 					PutNextCode(op, CODE_CLEAR);
1039c2c66affSColin Finck 					nbits = BITS_MIN;
1040c2c66affSColin Finck 					maxcode = MAXCODE(BITS_MIN);
1041c2c66affSColin Finck 				} else
1042c2c66affSColin Finck 					sp->enc_ratio = rat;
1043c2c66affSColin Finck 			}
1044c2c66affSColin Finck 		}
1045c2c66affSColin Finck 	hit:
1046c2c66affSColin Finck 		;
1047c2c66affSColin Finck 	}
1048c2c66affSColin Finck 
1049c2c66affSColin Finck 	/*
1050c2c66affSColin Finck 	 * Restore global state.
1051c2c66affSColin Finck 	 */
1052c2c66affSColin Finck 	sp->enc_incount = incount;
1053c2c66affSColin Finck 	sp->enc_outcount = outcount;
1054c2c66affSColin Finck 	sp->enc_checkpoint = checkpoint;
1055c2c66affSColin Finck 	sp->enc_oldcode = ent;
1056c2c66affSColin Finck 	sp->lzw_nextdata = nextdata;
1057c2c66affSColin Finck 	sp->lzw_nextbits = nextbits;
1058c2c66affSColin Finck 	sp->lzw_free_ent = (unsigned short)free_ent;
1059c2c66affSColin Finck 	sp->lzw_maxcode = (unsigned short)maxcode;
1060c2c66affSColin Finck 	sp->lzw_nbits = (unsigned short)nbits;
1061c2c66affSColin Finck 	tif->tif_rawcp = op;
1062c2c66affSColin Finck 	return (1);
1063c2c66affSColin Finck }
1064c2c66affSColin Finck 
1065c2c66affSColin Finck /*
1066c2c66affSColin Finck  * Finish off an encoded strip by flushing the last
1067c2c66affSColin Finck  * string and tacking on an End Of Information code.
1068c2c66affSColin Finck  */
1069c2c66affSColin Finck static int
LZWPostEncode(TIFF * tif)1070c2c66affSColin Finck LZWPostEncode(TIFF* tif)
1071c2c66affSColin Finck {
1072c2c66affSColin Finck 	register LZWCodecState *sp = EncoderState(tif);
1073c2c66affSColin Finck 	uint8* op = tif->tif_rawcp;
1074c2c66affSColin Finck 	long nextbits = sp->lzw_nextbits;
1075c2c66affSColin Finck 	unsigned long nextdata = sp->lzw_nextdata;
1076c2c66affSColin Finck 	long outcount = sp->enc_outcount;
1077c2c66affSColin Finck 	int nbits = sp->lzw_nbits;
1078c2c66affSColin Finck 
1079c2c66affSColin Finck 	if (op > sp->enc_rawlimit) {
1080c2c66affSColin Finck 		tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1081c2c66affSColin Finck 		if( !TIFFFlushData1(tif) )
1082c2c66affSColin Finck                     return 0;
1083c2c66affSColin Finck 		op = tif->tif_rawdata;
1084c2c66affSColin Finck 	}
1085c2c66affSColin Finck 	if (sp->enc_oldcode != (hcode_t) -1) {
1086c2c66affSColin Finck                 int free_ent = sp->lzw_free_ent;
1087c2c66affSColin Finck 
1088c2c66affSColin Finck 		PutNextCode(op, sp->enc_oldcode);
1089c2c66affSColin Finck 		sp->enc_oldcode = (hcode_t) -1;
1090c2c66affSColin Finck                 free_ent ++;
1091c2c66affSColin Finck 
1092c2c66affSColin Finck                 if (free_ent == CODE_MAX-1) {
1093c2c66affSColin Finck                         /* table is full, emit clear code and reset */
1094c2c66affSColin Finck                         outcount = 0;
1095c2c66affSColin Finck                         PutNextCode(op, CODE_CLEAR);
1096c2c66affSColin Finck                         nbits = BITS_MIN;
1097c2c66affSColin Finck                 } else {
1098c2c66affSColin Finck                         /*
1099c2c66affSColin Finck                         * If the next entry is going to be too big for
1100c2c66affSColin Finck                         * the code size, then increase it, if possible.
1101c2c66affSColin Finck                         */
1102c2c66affSColin Finck                         if (free_ent > sp->lzw_maxcode) {
1103c2c66affSColin Finck                                 nbits++;
1104c2c66affSColin Finck                                 assert(nbits <= BITS_MAX);
1105c2c66affSColin Finck                         }
1106c2c66affSColin Finck                 }
1107c2c66affSColin Finck 	}
1108c2c66affSColin Finck 	PutNextCode(op, CODE_EOI);
1109c2c66affSColin Finck         /* Explicit 0xff masking to make icc -check=conversions happy */
1110c2c66affSColin Finck 	if (nextbits > 0)
1111c2c66affSColin Finck 		*op++ = (unsigned char)((nextdata << (8-nextbits))&0xff);
1112c2c66affSColin Finck 	tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1113c2c66affSColin Finck 	return (1);
1114c2c66affSColin Finck }
1115c2c66affSColin Finck 
1116c2c66affSColin Finck /*
1117c2c66affSColin Finck  * Reset encoding hash table.
1118c2c66affSColin Finck  */
1119c2c66affSColin Finck static void
cl_hash(LZWCodecState * sp)1120c2c66affSColin Finck cl_hash(LZWCodecState* sp)
1121c2c66affSColin Finck {
1122c2c66affSColin Finck 	register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
1123c2c66affSColin Finck 	register long i = HSIZE-8;
1124c2c66affSColin Finck 
1125c2c66affSColin Finck 	do {
1126c2c66affSColin Finck 		i -= 8;
1127c2c66affSColin Finck 		hp[-7].hash = -1;
1128c2c66affSColin Finck 		hp[-6].hash = -1;
1129c2c66affSColin Finck 		hp[-5].hash = -1;
1130c2c66affSColin Finck 		hp[-4].hash = -1;
1131c2c66affSColin Finck 		hp[-3].hash = -1;
1132c2c66affSColin Finck 		hp[-2].hash = -1;
1133c2c66affSColin Finck 		hp[-1].hash = -1;
1134c2c66affSColin Finck 		hp[ 0].hash = -1;
1135c2c66affSColin Finck 		hp -= 8;
1136c2c66affSColin Finck 	} while (i >= 0);
1137c2c66affSColin Finck 	for (i += 8; i > 0; i--, hp--)
1138c2c66affSColin Finck 		hp->hash = -1;
1139c2c66affSColin Finck }
1140c2c66affSColin Finck 
1141c2c66affSColin Finck static void
LZWCleanup(TIFF * tif)1142c2c66affSColin Finck LZWCleanup(TIFF* tif)
1143c2c66affSColin Finck {
1144c2c66affSColin Finck 	(void)TIFFPredictorCleanup(tif);
1145c2c66affSColin Finck 
1146c2c66affSColin Finck 	assert(tif->tif_data != 0);
1147c2c66affSColin Finck 
1148c2c66affSColin Finck 	if (DecoderState(tif)->dec_codetab)
1149c2c66affSColin Finck 		_TIFFfree(DecoderState(tif)->dec_codetab);
1150c2c66affSColin Finck 
1151c2c66affSColin Finck 	if (EncoderState(tif)->enc_hashtab)
1152c2c66affSColin Finck 		_TIFFfree(EncoderState(tif)->enc_hashtab);
1153c2c66affSColin Finck 
1154c2c66affSColin Finck 	_TIFFfree(tif->tif_data);
1155c2c66affSColin Finck 	tif->tif_data = NULL;
1156c2c66affSColin Finck 
1157c2c66affSColin Finck 	_TIFFSetDefaultCompressionState(tif);
1158c2c66affSColin Finck }
1159c2c66affSColin Finck 
1160c2c66affSColin Finck int
TIFFInitLZW(TIFF * tif,int scheme)1161c2c66affSColin Finck TIFFInitLZW(TIFF* tif, int scheme)
1162c2c66affSColin Finck {
1163c2c66affSColin Finck 	static const char module[] = "TIFFInitLZW";
1164c2c66affSColin Finck 	assert(scheme == COMPRESSION_LZW);
1165c2c66affSColin Finck 	/*
1166c2c66affSColin Finck 	 * Allocate state block so tag methods have storage to record values.
1167c2c66affSColin Finck 	 */
1168c2c66affSColin Finck 	tif->tif_data = (uint8*) _TIFFmalloc(sizeof (LZWCodecState));
1169c2c66affSColin Finck 	if (tif->tif_data == NULL)
1170c2c66affSColin Finck 		goto bad;
1171c2c66affSColin Finck 	DecoderState(tif)->dec_codetab = NULL;
1172c2c66affSColin Finck 	DecoderState(tif)->dec_decode = NULL;
1173c2c66affSColin Finck 	EncoderState(tif)->enc_hashtab = NULL;
1174c2c66affSColin Finck         LZWState(tif)->rw_mode = tif->tif_mode;
1175c2c66affSColin Finck 
1176c2c66affSColin Finck 	/*
1177c2c66affSColin Finck 	 * Install codec methods.
1178c2c66affSColin Finck 	 */
1179c2c66affSColin Finck 	tif->tif_fixuptags = LZWFixupTags;
1180c2c66affSColin Finck 	tif->tif_setupdecode = LZWSetupDecode;
1181c2c66affSColin Finck 	tif->tif_predecode = LZWPreDecode;
1182c2c66affSColin Finck 	tif->tif_decoderow = LZWDecode;
1183c2c66affSColin Finck 	tif->tif_decodestrip = LZWDecode;
1184c2c66affSColin Finck 	tif->tif_decodetile = LZWDecode;
1185c2c66affSColin Finck 	tif->tif_setupencode = LZWSetupEncode;
1186c2c66affSColin Finck 	tif->tif_preencode = LZWPreEncode;
1187c2c66affSColin Finck 	tif->tif_postencode = LZWPostEncode;
1188c2c66affSColin Finck 	tif->tif_encoderow = LZWEncode;
1189c2c66affSColin Finck 	tif->tif_encodestrip = LZWEncode;
1190c2c66affSColin Finck 	tif->tif_encodetile = LZWEncode;
1191c2c66affSColin Finck 	tif->tif_cleanup = LZWCleanup;
1192c2c66affSColin Finck 	/*
1193c2c66affSColin Finck 	 * Setup predictor setup.
1194c2c66affSColin Finck 	 */
1195c2c66affSColin Finck 	(void) TIFFPredictorInit(tif);
1196c2c66affSColin Finck 	return (1);
1197c2c66affSColin Finck bad:
1198c2c66affSColin Finck 	TIFFErrorExt(tif->tif_clientdata, module,
1199c2c66affSColin Finck 		     "No space for LZW state block");
1200c2c66affSColin Finck 	return (0);
1201c2c66affSColin Finck }
1202c2c66affSColin Finck 
1203c2c66affSColin Finck /*
1204c2c66affSColin Finck  * Copyright (c) 1985, 1986 The Regents of the University of California.
1205c2c66affSColin Finck  * All rights reserved.
1206c2c66affSColin Finck  *
1207c2c66affSColin Finck  * This code is derived from software contributed to Berkeley by
1208c2c66affSColin Finck  * James A. Woods, derived from original work by Spencer Thomas
1209c2c66affSColin Finck  * and Joseph Orost.
1210c2c66affSColin Finck  *
1211c2c66affSColin Finck  * Redistribution and use in source and binary forms are permitted
1212c2c66affSColin Finck  * provided that the above copyright notice and this paragraph are
1213c2c66affSColin Finck  * duplicated in all such forms and that any documentation,
1214c2c66affSColin Finck  * advertising materials, and other materials related to such
1215c2c66affSColin Finck  * distribution and use acknowledge that the software was developed
1216c2c66affSColin Finck  * by the University of California, Berkeley.  The name of the
1217c2c66affSColin Finck  * University may not be used to endorse or promote products derived
1218c2c66affSColin Finck  * from this software without specific prior written permission.
1219c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1220c2c66affSColin Finck  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1221c2c66affSColin Finck  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1222c2c66affSColin Finck  */
1223c2c66affSColin Finck #endif /* LZW_SUPPORT */
1224c2c66affSColin Finck 
1225c2c66affSColin Finck /* vim: set ts=8 sts=8 sw=8 noet: */
1226c2c66affSColin Finck /*
1227c2c66affSColin Finck  * Local Variables:
1228c2c66affSColin Finck  * mode: c
1229c2c66affSColin Finck  * c-basic-offset: 8
1230c2c66affSColin Finck  * fill-column: 78
1231c2c66affSColin Finck  * End:
1232c2c66affSColin Finck  */
1233