"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* $Header: /cvsroot/libtiff-lzw-compression-kit/tif_lzw.c,v 1.4 2003/11/03 14:47:36 dron 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 "tiffiop.h"
28 #ifdef LZW_SUPPORT
29 /*
30 * TIFF Library.
31 * Rev 5.0 Lempel-Ziv & Welch Compression Support
32 *
33 * This code is derived from the compress program whose code is
34 * derived from software contributed to Berkeley by James A. Woods,
35 * derived from original work by Spencer Thomas and Joseph Orost.
36 *
37 * The original Berkeley copyright notice appears below in its entirety.
38 */
39 #include "tif_predict.h"
40
41 #include <assert.h>
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 u_short nbits; /* # of bits/code */
96 u_short maxcode; /* maximum code for lzw_nbits */
97 u_short free_ent; /* next free entry in hash table */
98 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 u_short length; /* string len, including this token */
125 u_char value; /* data value */
126 u_char firstchar; /* first token of string */
127 } code_t;
128
129 typedef int (*decodeFunc)(TIFF*, tidata_t, tsize_t, tsample_t);
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 long 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 tidata_t 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*, tidata_t, tsize_t, tsample_t);
163 #ifdef LZW_COMPAT
164 static int LZWDecodeCompat(TIFF*, tidata_t, tsize_t, tsample_t);
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 < nbits) { \
179 TIFFWarning(_tif->tif_name, \
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 LZWSetupDecode(TIFF* tif)
194 {
195 LZWCodecState* sp = DecoderState(tif);
196 static const char module[] = " LZWSetupDecode";
197 int code;
198
199 if( sp == NULL )
200 {
201 /*
202 * Allocate state block so tag methods have storage to record
203 * values.
204 */
205 tif->tif_data = (tidata_t) _TIFFmalloc(sizeof(LZWCodecState));
206 if (tif->tif_data == NULL)
207 {
208 TIFFError("LZWPreDecode", "No space for LZW state block");
209 return (0);
210 }
211
212 DecoderState(tif)->dec_codetab = NULL;
213 DecoderState(tif)->dec_decode = NULL;
214
215 /*
216 * Setup predictor setup.
217 */
218 (void) TIFFPredictorInit(tif);
219
220 sp = DecoderState(tif);
221 }
222
223 assert(sp != NULL);
224
225 if (sp->dec_codetab == NULL) {
226 sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
227 if (sp->dec_codetab == NULL) {
228 TIFFError(module, "No space for LZW code table");
229 return (0);
230 }
231 /*
232 * Pre-load the table.
233 */
234 code = 255;
235 do {
236 sp->dec_codetab[code].value = code;
237 sp->dec_codetab[code].firstchar = code;
238 sp->dec_codetab[code].length = 1;
239 sp->dec_codetab[code].next = NULL;
240 } while (code--);
241 }
242 return (1);
243 }
244
245 /*
246 * Setup state for decoding a strip.
247 */
248 static int
249 LZWPreDecode(TIFF* tif, tsample_t s)
250 {
251 LZWCodecState *sp = DecoderState(tif);
252
253 (void) s;
254 assert(sp != NULL);
255 /*
256 * Check for old bit-reversed codes.
257 */
258 if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
259 #ifdef LZW_COMPAT
260 if (!sp->dec_decode) {
261 TIFFWarning(tif->tif_name,
262 "Old-style LZW codes, convert file");
263 /*
264 * Override default decoding methods with
265 * ones that deal with the old coding.
266 * Otherwise the predictor versions set
267 * above will call the compatibility routines
268 * through the dec_decode method.
269 */
270 tif->tif_decoderow = LZWDecodeCompat;
271 tif->tif_decodestrip = LZWDecodeCompat;
272 tif->tif_decodetile = LZWDecodeCompat;
273 /*
274 * If doing horizontal differencing, must
275 * re-setup the predictor logic since we
276 * switched the basic decoder methods...
277 */
278 (*tif->tif_setupdecode)(tif);
279 sp->dec_decode = LZWDecodeCompat;
280 }
281 sp->lzw_maxcode = MAXCODE(BITS_MIN);
282 #else /* !LZW_COMPAT */
283 if (!sp->dec_decode) {
284 TIFFError(tif->tif_name,
285 "Old-style LZW codes not supported");
286 sp->dec_decode = LZWDecode;
287 }
288 return (0);
289 #endif/* !LZW_COMPAT */
290 } else {
291 sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
292 sp->dec_decode = LZWDecode;
293 }
294 sp->lzw_nbits = BITS_MIN;
295 sp->lzw_nextbits = 0;
296 sp->lzw_nextdata = 0;
297
298 sp->dec_restart = 0;
299 sp->dec_nbitsmask = MAXCODE(BITS_MIN);
300 #ifdef LZW_CHECKEOS
301 sp->dec_bitsleft = tif->tif_rawcc << 3;
302 #endif
303 sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
304 /*
305 * Zero entries that are not yet filled in. We do
306 * this to guard against bogus input data that causes
307 * us to index into undefined entries. If you can
308 * come up with a way to safely bounds-check input codes
309 * while decoding then you can remove this operation.
310 */
311 _TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
312 sp->dec_oldcodep = &sp->dec_codetab[-1];
313 sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
314 return (1);
315 }
316
317 /*
318 * Decode a "hunk of data".
319 */
320 #define GetNextCode(sp, bp, code) { \
321 nextdata = (nextdata<<8) | *(bp)++; \
322 nextbits += 8; \
323 if (nextbits < nbits) { \
324 nextdata = (nextdata<<8) | *(bp)++; \
325 nextbits += 8; \
326 } \
327 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
328 nextbits -= nbits; \
329 }
330
331 static void
332 codeLoop(TIFF* tif)
333 {
334 TIFFError(tif->tif_name,
335 "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
336 tif->tif_row);
337 }
338
339 static int
340 LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
341 {
342 LZWCodecState *sp = DecoderState(tif);
343 char *op = (char*) op0;
344 long occ = (long) occ0;
345 char *tp;
346 u_char *bp;
347 hcode_t code;
348 int len;
349 long nbits, nextbits, nextdata, nbitsmask;
350 code_t *codep, *free_entp, *maxcodep, *oldcodep;
351
352 (void) s;
353 assert(sp != NULL);
354 /*
355 * Restart interrupted output operation.
356 */
357 if (sp->dec_restart) {
358 long residue;
359
360 codep = sp->dec_codep;
361 residue = codep->length - sp->dec_restart;
362 if (residue > occ) {
363 /*
364 * Residue from previous decode is sufficient
365 * to satisfy decode request. Skip to the
366 * start of the decoded string, place decoded
367 * values in the output buffer, and return.
368 */
369 sp->dec_restart += occ;
370 do {
371 codep = codep->next;
372 } while (--residue > occ && codep);
373 if (codep) {
374 tp = op + occ;
375 do {
376 *--tp = codep->value;
377 codep = codep->next;
378 } while (--occ && codep);
379 }
380 return (1);
381 }
382 /*
383 * Residue satisfies only part of the decode request.
384 */
385 op += residue, occ -= residue;
386 tp = op;
387 do {
388 int t;
389 --tp;
390 t = codep->value;
391 codep = codep->next;
392 *tp = t;
393 } while (--residue && codep);
394 sp->dec_restart = 0;
395 }
396
397 bp = (u_char *)tif->tif_rawcp;
398 nbits = sp->lzw_nbits;
399 nextdata = sp->lzw_nextdata;
400 nextbits = sp->lzw_nextbits;
401 nbitsmask = sp->dec_nbitsmask;
402 oldcodep = sp->dec_oldcodep;
403 free_entp = sp->dec_free_entp;
404 maxcodep = sp->dec_maxcodep;
405
406 while (occ > 0) {
407 NextCode(tif, sp, bp, code, GetNextCode);
408 if (code == CODE_EOI)
409 break;
410 if (code == CODE_CLEAR) {
411 free_entp = sp->dec_codetab + CODE_FIRST;
412 nbits = BITS_MIN;
413 nbitsmask = MAXCODE(BITS_MIN);
414 maxcodep = sp->dec_codetab + nbitsmask-1;
415 NextCode(tif, sp, bp, code, GetNextCode);
416 if (code == CODE_EOI)
417 break;
418 *op++ = code, occ--;
419 oldcodep = sp->dec_codetab + code;
420 continue;
421 }
422 codep = sp->dec_codetab + code;
423
424 /*
425 * Add the new entry to the code table.
426 */
427 if (free_entp < &sp->dec_codetab[0] ||
428 free_entp >= &sp->dec_codetab[CSIZE]) {
429 TIFFError(tif->tif_name,
430 "LZWDecode: Corrupted LZW table at scanline %d",
431 tif->tif_row);
432 return (0);
433 }
434
435 free_entp->next = oldcodep;
436 if (free_entp->next < &sp->dec_codetab[0] ||
437 free_entp->next >= &sp->dec_codetab[CSIZE]) {
438 TIFFError(tif->tif_name,
439 "LZWDecode: Corrupted LZW table at scanline %d",
440 tif->tif_row);
441 return (0);
442 }
443 free_entp->firstchar = free_entp->next->firstchar;
444 free_entp->length = free_entp->next->length+1;
445 free_entp->value = (codep < free_entp) ?
446 codep->firstchar : free_entp->firstchar;
447 if (++free_entp > maxcodep) {
448 if (++nbits > BITS_MAX) /* should not happen */
449 nbits = BITS_MAX;
450 nbitsmask = MAXCODE(nbits);
451 maxcodep = sp->dec_codetab + nbitsmask-1;
452 }
453 oldcodep = codep;
454 if (code >= 256) {
455 /*
456 * Code maps to a string, copy string
457 * value to output (written in reverse).
458 */
459 if(codep->length == 0) {
460 TIFFError(tif->tif_name,
461 "LZWDecode: Wrong length of decoded string: "
462 "data probably corrupted at scanline %d",
463 tif->tif_row);
464 return (0);
465 }
466 if (codep->length > occ) {
467 /*
468 * String is too long for decode buffer,
469 * locate portion that will fit, copy to
470 * the decode buffer, and setup restart
471 * logic for the next decoding call.
472 */
473 sp->dec_codep = codep;
474 do {
475 codep = codep->next;
476 } while (codep && codep->length > occ);
477 if (codep) {
478 sp->dec_restart = occ;
479 tp = op + occ;
480 do {
481 *--tp = codep->value;
482 codep = codep->next;
483 } while (--occ && codep);
484 if (codep)
485 codeLoop(tif);
486 }
487 break;
488 }
489 len = codep->length;
490 tp = op + len;
491 do {
492 int t;
493 --tp;
494 t = codep->value;
495 codep = codep->next;
496 *tp = t;
497 } while (codep && tp > op);
498 if (codep) {
499 codeLoop(tif);
500 break;
501 }
502 op += len, occ -= len;
503 } else
504 *op++ = code, occ--;
505 }
506
507 tif->tif_rawcp = (tidata_t) bp;
508 sp->lzw_nbits = (u_short) nbits;
509 sp->lzw_nextdata = nextdata;
510 sp->lzw_nextbits = nextbits;
511 sp->dec_nbitsmask = nbitsmask;
512 sp->dec_oldcodep = oldcodep;
513 sp->dec_free_entp = free_entp;
514 sp->dec_maxcodep = maxcodep;
515
516 if (occ > 0) {
517 TIFFError(tif->tif_name,
518 "LZWDecode: Not enough data at scanline %d (short %d bytes)",
519 tif->tif_row, occ);
520 return (0);
521 }
522 return (1);
523 }
524
525 #ifdef LZW_COMPAT
526 /*
527 * Decode a "hunk of data" for old images.
528 */
529 #define GetNextCodeCompat(sp, bp, code) { \
530 nextdata |= (u_long) *(bp)++ << nextbits; \
531 nextbits += 8; \
532 if (nextbits < nbits) { \
533 nextdata |= (u_long) *(bp)++ << nextbits; \
534 nextbits += 8; \
535 } \
536 code = (hcode_t)(nextdata & nbitsmask); \
537 nextdata >>= nbits; \
538 nextbits -= nbits; \
539 }
540
541 static int
542 LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
543 {
544 LZWCodecState *sp = DecoderState(tif);
545 char *op = (char*) op0;
546 long occ = (long) occ0;
547 char *tp;
548 u_char *bp;
549 int code, nbits;
550 long nextbits, nextdata, nbitsmask;
551 code_t *codep, *free_entp, *maxcodep, *oldcodep;
552
553 (void) s;
554 assert(sp != NULL);
555 /*
556 * Restart interrupted output operation.
557 */
558 if (sp->dec_restart) {
559 long residue;
560
561 codep = sp->dec_codep;
562 residue = codep->length - sp->dec_restart;
563 if (residue > occ) {
564 /*
565 * Residue from previous decode is sufficient
566 * to satisfy decode request. Skip to the
567 * start of the decoded string, place decoded
568 * values in the output buffer, and return.
569 */
570 sp->dec_restart += occ;
571 do {
572 codep = codep->next;
573 } while (--residue > occ);
574 tp = op + occ;
575 do {
576 *--tp = codep->value;
577 codep = codep->next;
578 } while (--occ);
579 return (1);
580 }
581 /*
582 * Residue satisfies only part of the decode request.
583 */
584 op += residue, occ -= residue;
585 tp = op;
586 do {
587 *--tp = codep->value;
588 codep = codep->next;
589 } while (--residue);
590 sp->dec_restart = 0;
591 }
592
593 bp = (u_char *)tif->tif_rawcp;
594 nbits = sp->lzw_nbits;
595 nextdata = sp->lzw_nextdata;
596 nextbits = sp->lzw_nextbits;
597 nbitsmask = sp->dec_nbitsmask;
598 oldcodep = sp->dec_oldcodep;
599 free_entp = sp->dec_free_entp;
600 maxcodep = sp->dec_maxcodep;
601
602 while (occ > 0) {
603 NextCode(tif, sp, bp, code, GetNextCodeCompat);
604 if (code == CODE_EOI)
605 break;
606 if (code == CODE_CLEAR) {
607 free_entp = sp->dec_codetab + CODE_FIRST;
608 nbits = BITS_MIN;
609 nbitsmask = MAXCODE(BITS_MIN);
610 maxcodep = sp->dec_codetab + nbitsmask;
611 NextCode(tif, sp, bp, code, GetNextCodeCompat);
612 if (code == CODE_EOI)
613 break;
614 *op++ = code, occ--;
615 oldcodep = sp->dec_codetab + code;
616 continue;
617 }
618 codep = sp->dec_codetab + code;
619
620 /*
621 * Add the new entry to the code table.
622 */
623 if (free_entp < &sp->dec_codetab[0] ||
624 free_entp >= &sp->dec_codetab[CSIZE]) {
625 TIFFError(tif->tif_name,
626 "LZWDecodeCompat: Corrupted LZW table at scanline %d",
627 tif->tif_row);
628 return (0);
629 }
630
631 free_entp->next = oldcodep;
632 if (free_entp->next < &sp->dec_codetab[0] ||
633 free_entp->next >= &sp->dec_codetab[CSIZE]) {
634 TIFFError(tif->tif_name,
635 "LZWDecodeCompat: Corrupted LZW table at scanline %d",
636 tif->tif_row);
637 return (0);
638 }
639 free_entp->firstchar = free_entp->next->firstchar;
640 free_entp->length = free_entp->next->length+1;
641 free_entp->value = (codep < free_entp) ?
642 codep->firstchar : free_entp->firstchar;
643 if (++free_entp > maxcodep) {
644 if (++nbits > BITS_MAX) /* should not happen */
645 nbits = BITS_MAX;
646 nbitsmask = MAXCODE(nbits);
647 maxcodep = sp->dec_codetab + nbitsmask;
648 }
649 oldcodep = codep;
650 if (code >= 256) {
651 /*
652 * Code maps to a string, copy string
653 * value to output (written in reverse).
654 */
655 if(codep->length == 0) {
656 TIFFError(tif->tif_name,
657 "LZWDecodeCompat: Wrong length of decoded "
658 "string: data probably corrupted at scanline %d",
659 tif->tif_row);
660 return (0);
661 }
662 if (codep->length > occ) {
663 /*
664 * String is too long for decode buffer,
665 * locate portion that will fit, copy to
666 * the decode buffer, and setup restart
667 * logic for the next decoding call.
668 */
669 sp->dec_codep = codep;
670 do {
671 codep = codep->next;
672 } while (codep->length > occ);
673 sp->dec_restart = occ;
674 tp = op + occ;
675 do {
676 *--tp = codep->value;
677 codep = codep->next;
678 } while (--occ);
679 break;
680 }
681 op += codep->length, occ -= codep->length;
682 tp = op;
683 do {
684 *--tp = codep->value;
685 } while( (codep = codep->next) != NULL);
686 } else
687 *op++ = code, occ--;
688 }
689
690 tif->tif_rawcp = (tidata_t) bp;
691 sp->lzw_nbits = nbits;
692 sp->lzw_nextdata = nextdata;
693 sp->lzw_nextbits = nextbits;
694 sp->dec_nbitsmask = nbitsmask;
695 sp->dec_oldcodep = oldcodep;
696 sp->dec_free_entp = free_entp;
697 sp->dec_maxcodep = maxcodep;
698
699 if (occ > 0) {
700 TIFFError(tif->tif_name,
701 "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
702 tif->tif_row, occ);
703 return (0);
704 }
705 return (1);
706 }
707 #endif /* LZW_COMPAT */
708
709 /*
710 * LZW Encoding.
711 */
712
713 static int
714 LZWSetupEncode(TIFF* tif)
715 {
716 LZWCodecState* sp = EncoderState(tif);
717 static const char module[] = "LZWSetupEncode";
718
719 assert(sp != NULL);
720 sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
721 if (sp->enc_hashtab == NULL) {
722 TIFFError(module, "No space for LZW hash table");
723 return (0);
724 }
725 return (1);
726 }
727
728 /*
729 * Reset encoding state at the start of a strip.
730 */
731 static int
732 LZWPreEncode(TIFF* tif, tsample_t s)
733 {
734 LZWCodecState *sp = EncoderState(tif);
735
736 (void) s;
737 assert(sp != NULL);
738 sp->lzw_nbits = BITS_MIN;
739 sp->lzw_maxcode = MAXCODE(BITS_MIN);
740 sp->lzw_free_ent = CODE_FIRST;
741 sp->lzw_nextbits = 0;
742 sp->lzw_nextdata = 0;
743 sp->enc_checkpoint = CHECK_GAP;
744 sp->enc_ratio = 0;
745 sp->enc_incount = 0;
746 sp->enc_outcount = 0;
747 /*
748 * The 4 here insures there is space for 2 max-sized
749 * codes in LZWEncode and LZWPostDecode.
750 */
751 sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
752 cl_hash(sp); /* clear hash table */
753 sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
754 return (1);
755 }
756
757 #define CALCRATIO(sp, rat) { \
758 if (incount > 0x007fffff) { /* NB: shift will overflow */\
759 rat = outcount >> 8; \
760 rat = (rat == 0 ? 0x7fffffff : incount/rat); \
761 } else \
762 rat = (incount<<8) / outcount; \
763 }
764 #define PutNextCode(op, c) { \
765 nextdata = (nextdata << nbits) | c; \
766 nextbits += nbits; \
767 *op++ = (u_char)(nextdata >> (nextbits-8)); \
768 nextbits -= 8; \
769 if (nextbits >= 8) { \
770 *op++ = (u_char)(nextdata >> (nextbits-8)); \
771 nextbits -= 8; \
772 } \
773 outcount += nbits; \
774 }
775
776 /*
777 * Encode a chunk of pixels.
778 *
779 * Uses an open addressing double hashing (no chaining) on the
780 * prefix code/next character combination. We do a variant of
781 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
782 * relatively-prime secondary probe. Here, the modular division
783 * first probe is gives way to a faster exclusive-or manipulation.
784 * Also do block compression with an adaptive reset, whereby the
785 * code table is cleared when the compression ratio decreases,
786 * but after the table fills. The variable-length output codes
787 * are re-sized at this point, and a CODE_CLEAR is generated
788 * for the decoder.
789 */
790 static int
791 LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
792 {
793 register LZWCodecState *sp = EncoderState(tif);
794 register long fcode;
795 register hash_t *hp;
796 register int h, c;
797 hcode_t ent;
798 long disp;
799 long incount, outcount, checkpoint;
800 long nextdata, nextbits;
801 int free_ent, maxcode, nbits;
802 tidata_t op, limit;
803
804 (void) s;
805 if (sp == NULL)
806 return (0);
807 /*
808 * Load local state.
809 */
810 incount = sp->enc_incount;
811 outcount = sp->enc_outcount;
812 checkpoint = sp->enc_checkpoint;
813 nextdata = sp->lzw_nextdata;
814 nextbits = sp->lzw_nextbits;
815 free_ent = sp->lzw_free_ent;
816 maxcode = sp->lzw_maxcode;
817 nbits = sp->lzw_nbits;
818 op = tif->tif_rawcp;
819 limit = sp->enc_rawlimit;
820 ent = sp->enc_oldcode;
821
822 if (ent == (hcode_t) -1 && cc > 0) {
823 /*
824 * NB: This is safe because it can only happen
825 * at the start of a strip where we know there
826 * is space in the data buffer.
827 */
828 PutNextCode(op, CODE_CLEAR);
829 ent = *bp++; cc--; incount++;
830 }
831 while (cc > 0) {
832 c = *bp++; cc--; incount++;
833 fcode = ((long)c << BITS_MAX) + ent;
834 h = (c << HSHIFT) ^ ent; /* xor hashing */
835 #ifdef _WINDOWS
836 /*
837 * Check hash index for an overflow.
838 */
839 if (h >= HSIZE)
840 h -= HSIZE;
841 #endif
842 hp = &sp->enc_hashtab[h];
843 if (hp->hash == fcode) {
844 ent = hp->code;
845 continue;
846 }
847 if (hp->hash >= 0) {
848 /*
849 * Primary hash failed, check secondary hash.
850 */
851 disp = HSIZE - h;
852 if (h == 0)
853 disp = 1;
854 do {
855 /*
856 * Avoid pointer arithmetic 'cuz of
857 * wraparound problems with segments.
858 */
859 if ((h -= disp) < 0)
860 h += HSIZE;
861 hp = &sp->enc_hashtab[h];
862 if (hp->hash == fcode) {
863 ent = hp->code;
864 goto hit;
865 }
866 } while (hp->hash >= 0);
867 }
868 /*
869 * New entry, emit code and add to table.
870 */
871 /*
872 * Verify there is space in the buffer for the code
873 * and any potential Clear code that might be emitted
874 * below. The value of limit is setup so that there
875 * are at least 4 bytes free--room for 2 codes.
876 */
877 if (op > limit) {
878 tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
879 TIFFFlushData1(tif);
880 op = tif->tif_rawdata;
881 }
882 PutNextCode(op, ent);
883 ent = c;
884 hp->code = free_ent++;
885 hp->hash = fcode;
886 if (free_ent == CODE_MAX-1) {
887 /* table is full, emit clear code and reset */
888 cl_hash(sp);
889 sp->enc_ratio = 0;
890 incount = 0;
891 outcount = 0;
892 free_ent = CODE_FIRST;
893 PutNextCode(op, CODE_CLEAR);
894 nbits = BITS_MIN;
895 maxcode = MAXCODE(BITS_MIN);
896 } else {
897 /*
898 * If the next entry is going to be too big for
899 * the code size, then increase it, if possible.
900 */
901 if (free_ent > maxcode) {
902 nbits++;
903 assert(nbits <= BITS_MAX);
904 maxcode = (int) MAXCODE(nbits);
905 } else if (incount >= checkpoint) {
906 long rat;
907 /*
908 * Check compression ratio and, if things seem
909 * to be slipping, clear the hash table and
910 * reset state. The compression ratio is a
911 * 24+8-bit fractional number.
912 */
913 checkpoint = incount+CHECK_GAP;
914 CALCRATIO(sp, rat);
915 if (rat <= sp->enc_ratio) {
916 cl_hash(sp);
917 sp->enc_ratio = 0;
918 incount = 0;
919 outcount = 0;
920 free_ent = CODE_FIRST;
921 PutNextCode(op, CODE_CLEAR);
922 nbits = BITS_MIN;
923 maxcode = MAXCODE(BITS_MIN);
924 } else
925 sp->enc_ratio = rat;
926 }
927 }
928 hit:
929 ;
930 }
931
932 /*
933 * Restore global state.
934 */
935 sp->enc_incount = incount;
936 sp->enc_outcount = outcount;
937 sp->enc_checkpoint = checkpoint;
938 sp->enc_oldcode = ent;
939 sp->lzw_nextdata = nextdata;
940 sp->lzw_nextbits = nextbits;
941 sp->lzw_free_ent = free_ent;
942 sp->lzw_maxcode = maxcode;
943 sp->lzw_nbits = nbits;
944 tif->tif_rawcp = op;
945 return (1);
946 }
947
948 /*
949 * Finish off an encoded strip by flushing the last
950 * string and tacking on an End Of Information code.
951 */
952 static int
953 LZWPostEncode(TIFF* tif)
954 {
955 register LZWCodecState *sp = EncoderState(tif);
956 tidata_t op = tif->tif_rawcp;
957 long nextbits = sp->lzw_nextbits;
958 long nextdata = sp->lzw_nextdata;
959 long outcount = sp->enc_outcount;
960 int nbits = sp->lzw_nbits;
961
962 if (op > sp->enc_rawlimit) {
963 tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
964 TIFFFlushData1(tif);
965 op = tif->tif_rawdata;
966 }
967 if (sp->enc_oldcode != (hcode_t) -1) {
968 PutNextCode(op, sp->enc_oldcode);
969 sp->enc_oldcode = (hcode_t) -1;
970 }
971 PutNextCode(op, CODE_EOI);
972 if (nextbits > 0)
973 *op++ = (u_char)(nextdata << (8-nextbits));
974 tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
975 return (1);
976 }
977
978 /*
979 * Reset encoding hash table.
980 */
981 static void
982 cl_hash(LZWCodecState* sp)
983 {
984 register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
985 register long i = HSIZE-8;
986
987 do {
988 i -= 8;
989 hp[-7].hash = -1;
990 hp[-6].hash = -1;
991 hp[-5].hash = -1;
992 hp[-4].hash = -1;
993 hp[-3].hash = -1;
994 hp[-2].hash = -1;
995 hp[-1].hash = -1;
996 hp[ 0].hash = -1;
997 hp -= 8;
998 } while (i >= 0);
999 for (i += 8; i > 0; i--, hp--)
1000 hp->hash = -1;
1001 }
1002
1003 static void
1004 LZWCleanup(TIFF* tif)
1005 {
1006 if (tif->tif_data) {
1007 if (DecoderState(tif)->dec_codetab)
1008 _TIFFfree(DecoderState(tif)->dec_codetab);
1009
1010 if (EncoderState(tif)->enc_hashtab)
1011 _TIFFfree(EncoderState(tif)->enc_hashtab);
1012
1013 _TIFFfree(tif->tif_data);
1014 tif->tif_data = NULL;
1015 }
1016 }
1017
1018 int
1019 TIFFInitLZW(TIFF* tif, int scheme)
1020 {
1021 assert(scheme == COMPRESSION_LZW);
1022 /*
1023 * Allocate state block so tag methods have storage to record values.
1024 */
1025 tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LZWCodecState));
1026 if (tif->tif_data == NULL)
1027 goto bad;
1028 DecoderState(tif)->dec_codetab = NULL;
1029 DecoderState(tif)->dec_decode = NULL;
1030 EncoderState(tif)->enc_hashtab = NULL;
1031 LZWState(tif)->rw_mode = tif->tif_mode;
1032
1033 /*
1034 * Install codec methods.
1035 */
1036 tif->tif_setupdecode = LZWSetupDecode;
1037 tif->tif_predecode = LZWPreDecode;
1038 tif->tif_decoderow = LZWDecode;
1039 tif->tif_decodestrip = LZWDecode;
1040 tif->tif_decodetile = LZWDecode;
1041 tif->tif_setupencode = LZWSetupEncode;
1042 tif->tif_preencode = LZWPreEncode;
1043 tif->tif_postencode = LZWPostEncode;
1044 tif->tif_encoderow = LZWEncode;
1045 tif->tif_encodestrip = LZWEncode;
1046 tif->tif_encodetile = LZWEncode;
1047 tif->tif_cleanup = LZWCleanup;
1048 /*
1049 * Setup predictor setup.
1050 */
1051 (void) TIFFPredictorInit(tif);
1052 return (1);
1053 bad:
1054 TIFFError("TIFFInitLZW", "No space for LZW state block");
1055 return (0);
1056 }
1057
1058 /*
1059 * Copyright (c) 1985, 1986 The Regents of the University of California.
1060 * All rights reserved.
1061 *
1062 * This code is derived from software contributed to Berkeley by
1063 * James A. Woods, derived from original work by Spencer Thomas
1064 * and Joseph Orost.
1065 *
1066 * Redistribution and use in source and binary forms are permitted
1067 * provided that the above copyright notice and this paragraph are
1068 * duplicated in all such forms and that any documentation,
1069 * advertising materials, and other materials related to such
1070 * distribution and use acknowledge that the software was developed
1071 * by the University of California, Berkeley. The name of the
1072 * University may not be used to endorse or promote products derived
1073 * from this software without specific prior written permission.
1074 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1075 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1076 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1077 */
1078 #endif /* LZW_SUPPORT */