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