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