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