Skip to content

Commit 4af6400

Browse files
committed
Fix libtiff 4.0.8 regression when reading LZW-compressed strips with scanline API
Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2800
1 parent 48e2696 commit 4af6400

7 files changed

+27
-4
lines changed

libtiff/tif_config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
/* Define to 1 if you have the `lfind' function. */
7272
#undef HAVE_LFIND
7373

74+
/* Define to 1 if you have the <memory.h> header file. */
75+
#undef HAVE_MEMORY_H
76+
7477
/* Define to 1 if you have the `mmap' function. */
7578
#undef HAVE_MMAP
7679

libtiff/tif_lzw.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ typedef struct {
133133
long dec_restart; /* restart count */
134134
#ifdef LZW_CHECKEOS
135135
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 */
136137
#endif
137138
decodeFunc dec_decode; /* regular or backwards compatible */
138139
code_t* dec_codep; /* current recognized code */
@@ -318,6 +319,7 @@ LZWPreDecode(TIFF* tif, uint16 s)
318319
sp->dec_nbitsmask = MAXCODE(BITS_MIN);
319320
#ifdef LZW_CHECKEOS
320321
sp->dec_bitsleft = 0;
322+
sp->old_tif_rawcc = 0;
321323
#endif
322324
sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
323325
/*
@@ -425,7 +427,7 @@ LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
425427

426428
bp = (unsigned char *)tif->tif_rawcp;
427429
#ifdef LZW_CHECKEOS
428-
sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
430+
sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
429431
#endif
430432
nbits = sp->lzw_nbits;
431433
nextdata = sp->lzw_nextdata;
@@ -553,6 +555,9 @@ LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
553555

554556
tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
555557
tif->tif_rawcp = (uint8*) bp;
558+
#ifdef LZW_CHECKEOS
559+
sp->old_tif_rawcc = tif->tif_rawcc;
560+
#endif
556561
sp->lzw_nbits = (unsigned short) nbits;
557562
sp->lzw_nextdata = nextdata;
558563
sp->lzw_nextbits = nextbits;
@@ -656,7 +661,7 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
656661

657662
bp = (unsigned char *)tif->tif_rawcp;
658663
#ifdef LZW_CHECKEOS
659-
sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
664+
sp->dec_bitsleft += (((uint64)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
660665
#endif
661666
nbits = sp->lzw_nbits;
662667
nextdata = sp->lzw_nextdata;
@@ -774,6 +779,9 @@ LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
774779

775780
tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
776781
tif->tif_rawcp = (uint8*) bp;
782+
#ifdef LZW_CHECKEOS
783+
sp->old_tif_rawcc = tif->tif_rawcc;
784+
#endif
777785
sp->lzw_nbits = (unsigned short)nbits;
778786
sp->lzw_nextdata = nextdata;
779787
sp->lzw_nextbits = nextbits;

test/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(TESTSCRIPTS
4444
tiffcp-logluv.sh
4545
tiffcp-thumbnail.sh
4646
tiffcp-lzw-compat.sh
47+
tiffcp-lzw-scanline-decode.sh
4748
tiffdump.sh
4849
tiffinfo.sh
4950
tiffcp-split.sh
@@ -120,7 +121,8 @@ set(TIFFIMAGES
120121
images/rgb-3c-16b.tiff
121122
images/rgb-3c-8b.tiff
122123
images/quad-tile.jpg.tiff
123-
images/quad-lzw-compat.tiff)
124+
images/quad-lzw-compat.tiff
125+
images/lzw-single-strip.tiff)
124126

125127
set(BMPIMAGES
126128
images/palette-1c-8b.bmp
@@ -335,6 +337,7 @@ add_convert_test(tiffcp g32d "-c g3:2d" "images/miniswhite-1c-1b.ti
335337
add_convert_test(tiffcp g32dfill "-c g3:2d:fill" "images/miniswhite-1c-1b.tiff" FALSE)
336338
add_convert_test(tiffcp g4 "-c g4" "images/miniswhite-1c-1b.tiff" FALSE)
337339
add_convert_test(tiffcp none "-c none" "images/quad-lzw-compat.tiff" FALSE)
340+
add_convert_test(tiffcp noner1 "-c none -r 1" "images/lzw-single-strip.tiff" FALSE)
338341
add_convert_test_multi(tiffcp tiffcp "" logluv "-c none" "-c sgilog" ""
339342
"images/logluv-3c-16b.tiff" FALSE)
340343
add_convert_test_multi(tiffcp thumbnail "" thumbnail "g3:1d" "" ""

test/Makefile.am

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ TESTSCRIPTS = \
7979
tiffcp-logluv.sh \
8080
tiffcp-thumbnail.sh \
8181
tiffcp-lzw-compat.sh \
82+
tiffcp-lzw-scanline-decode.sh \
8283
tiffdump.sh \
8384
tiffinfo.sh \
8485
tiffcp-split.sh \
@@ -158,7 +159,8 @@ TIFFIMAGES = \
158159
images/rgb-3c-16b.tiff \
159160
images/rgb-3c-8b.tiff \
160161
images/quad-tile.jpg.tiff \
161-
images/quad-lzw-compat.tiff
162+
images/quad-lzw-compat.tiff \
163+
images/lzw-single-strip.tiff
162164

163165
PNMIMAGES = \
164166
images/minisblack-1c-8b.pgm \

test/common.sh

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ IMG_RGB_3C_16B=${IMAGES}/rgb-3c-16b.tiff
4141
IMG_RGB_3C_8B=${IMAGES}/rgb-3c-8b.tiff
4242
IMG_MINISBLACK_2C_8B_ALPHA=${IMAGES}/minisblack-2c-8b-alpha.tiff
4343
IMG_QUAD_LZW_COMPAT=${IMAGES}/quad-lzw-compat.tiff
44+
IMG_LZW_SINGLE_STROP=${IMAGES}/lzw-single-strip.tiff
4445

4546
IMG_MINISWHITE_1C_1B_PBM=${IMAGES}/miniswhite-1c-1b.pbm
4647
IMG_MINISBLACK_1C_8B_PGM=${IMAGES}/minisblack-1c-8b.pgm

test/images/lzw-single-strip.tiff

74.5 KB
Binary file not shown.

test/tiffcp-lzw-scanline-decode.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
#
3+
# Basic sanity check for tiffcp with LZW decompression
4+
#
5+
. ${srcdir:-.}/common.sh
6+
f_test_convert "${TIFFCP} -c none -r 1" "${IMG_LZW_SINGLE_STROP}" "o-tiffcp-lzw-scanline-decode.tiff"

0 commit comments

Comments
 (0)