Return-Path: <tiff-bounces@remotesensing.org>
Received: from xserve.flids.com (adsl-065-083-169-202.sip.mco.bellsouth.net [65.83.169.202])
        by inmx005.isp.belgacom.be (8.12.11/8.12.11/Skynet-IN-2.32) with ESMTP id j5HJoLHX013677
        for <joris.at.lebbeke@skynet.be>; Fri, 17 Jun 2005 21:50:26 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from adsl-065-083-169-202.sip.mco.bellsouth.net (localhost [127.0.0.1])
	by xserve.flids.com (Postfix) with ESMTP
	id CCDF42635E1E; Fri, 17 Jun 2005 15:49:45 -0400 (EDT)
X-Original-To: tiff@remotesensing.org
Delivered-To: tiff@remotesensing.org
Received: from smtp2.globetrotter.net (smtp2.globetrotter.net [142.169.1.152])
	by xserve.flids.com (Postfix) with ESMTP id F36322635DF5
	for <tiff@remotesensing.org>; Fri, 17 Jun 2005 15:49:42 -0400 (EDT)
Received: from (192.168.250.24) by avas7.globetrotter.net via smtp
	id 1486_f2d7841a_df68_11d9_9862_0002b3e6f1b0;
	Fri, 17 Jun 2005 15:49:43 -0400
Received: from [192.168.1.5]
	(c207.134.23-177.clta.globetrotter.net [207.134.23.177])
	by smtp2.globetrotter.net (iPlanet Messaging Server 5.2)
	with ESMTPA id <0II800B8ZV2TXB@"TELUS Quebec"> for
	tiff@remotesensing.org; Fri, 17 Jun 2005 15:49:42 -0400 (EDT)
Received: from (207.134.23.177) by avas7.globetrotter.net via smtp	id
	147f_f22a699c_df68_11d9_9ccf_0002b3e6f1b0;
	Fri, 17 Jun 2005 15:49:41 -0400
Date: Fri, 17 Jun 2005 15:49:40 -0400
From: Steve Roy <sroy@mac.com>
To: tiff@remotesensing.org
Message-id: <5889EFD8-6BB1-4CDF-A499-2C00EE3CCE92@mac.com>
MIME-version: 1.0
X-Mailer: Apple Mail (2.730)
Subject: [Tiff] Fix for corrupted multipage TIFF
X-BeenThere: tiff@remotesensing.org
X-Mailman-Version: 2.1.2
Precedence: list
List-Id: TIFF and libtiff discussion  <tiff.remotesensing.org>
List-Unsubscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=unsubscribe>
List-Archive: <http://xserve.flids.com/pipermail/tiff>
List-Post: <mailto:tiff@remotesensing.org>
List-Help: <mailto:tiff-request@remotesensing.org?subject=help>
List-Subscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=subscribe>
Content-Type: multipart/mixed; boundary="===============44830490520644284=="
Sender: tiff-bounces@remotesensing.org
Errors-To: tiff-bounces@remotesensing.org
X-UIDL: 000000000023ceae.000003d8
X-MSP-STORE: Fri, 17 Jun 2005 21:50:41 +0200
Status:   


--===============44830490520644284==
Content-type: multipart/alternative;
	boundary="Boundary_(ID_VwWRb8lTWFid+eXeXpOdrQ)"


--Boundary_(ID_VwWRb8lTWFid+eXeXpOdrQ)
Content-type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
Content-transfer-encoding: 7BIT

Hi,

We use LibTiff to read multipage TIFFs. We found a case where the  
first page is invalid while the other pages are fine. Unfortunately  
TIFFOpen returns NULL in such a case because the code tries to load  
the first page and fails so it assumes everything else is bad. I  
found a fix that we would like to submit but not being that familiar  
with the code I would like to run it by you guys.

The fix is at the end of the TIFFClientOpen() function in tif_open.c.  
It goes like this, with my changes annotated by STEVE.

     /*
      * Setup initial directory.
      */
     switch (mode[0]) {
     case 'r':
         tif->tif_nextdiroff = tif->tif_header.tiff_diroff;
         /*
          * Try to use a memory-mapped file if the client
          * has not explicitly suppressed usage with the
          * 'm' flag in the open mode (see above).
          */
         if ((tif->tif_flags & TIFF_MAPPED) &&
     !TIFFMapFileContents(tif, (tdata_t*) &tif->tif_base, &tif- 
 >tif_size))
             tif->tif_flags &= ~TIFF_MAPPED;
         if (TIFFReadDirectory(tif)) {
             tif->tif_rawcc = -1;
             tif->tif_flags |= TIFF_BUFFERSETUP;
/*          return (tif); STEVE - Remove this line */
         }
/*      break; STEVE - Remove this line */
         return (tif); /* STEVE - Add this line */
     case 'a':
         /*
          * New directories are automatically append
          * to the end of the directory chain when they
          * are written out (see TIFFWriteDirectory).
          */
         if (!TIFFDefaultDirectory(tif))
             goto bad;
         return (tif);
     }

This change allows TIFFOpen to still return a TIFF, and even though  
the first image is unreadable, the other pages are readable and it  
seems to work just fine. Would this change break anything?

You can find the corrupted TIFF file I've been testing with here:

<http://www.roydesign.net/corrupted.tif>

Steve

-- 
Steve Roy <sroy@mac.com>
Personal homepage: <http://homepage.mac.com/sroy>
Projects homepage: <http://www.roydesign.net>


--Boundary_(ID_VwWRb8lTWFid+eXeXpOdrQ)
Content-type: text/html; charset=ISO-8859-1
Content-transfer-encoding: quoted-printable

<HTML><BODY style=3D"word-wrap: break-word; -khtml-nbsp-mode: space; =
-khtml-line-break: after-white-space; ">Hi,<DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV>We use LibTiff to read =
multipage TIFFs. We found a case where the first page is invalid while =
the other pages are fine. Unfortunately TIFFOpen returns NULL in such a =
case because the code tries to load the first page and fails so it =
assumes everything else is bad. I found a fix that we would like to =
submit but not being that familiar with the code I would like to run it =
by you guys.</DIV><DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV>The fix is at the end of =
the TIFFClientOpen() function in tif_open.c. It goes like this, with my =
changes annotated by STEVE.</DIV><DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV>=A0=A0 =A0/*</DIV><DIV>=A0 =
=A0=A0 * Setup initial directory.</DIV><DIV>=A0 =A0=A0 */</DIV><DIV>=A0 =
=A0 switch (mode[0]) {</DIV><DIV>=A0 =A0 case 'r':</DIV><DIV>=A0 =A0 =A0 =
=A0 tif-&gt;tif_nextdiroff =3D =
tif-&gt;tif_header.tiff_diroff;</DIV><DIV>=A0 =A0 =A0 =A0 /*</DIV><DIV>=A0=
 =A0 =A0 =A0=A0 * Try to use a memory-mapped file if the =
client</DIV><DIV>=A0 =A0 =A0 =A0=A0 * has not explicitly suppressed =
usage with the</DIV><DIV>=A0 =A0 =A0 =A0=A0 * 'm' flag in the open mode =
(see above).</DIV><DIV>=A0 =A0 =A0 =A0=A0 */</DIV><DIV>=A0 =A0 =A0 =A0 =
if ((tif-&gt;tif_flags &amp; TIFF_MAPPED) &amp;&amp;</DIV><DIV>=A0 =A0 =
!TIFFMapFileContents(tif, (tdata_t*) &amp;tif-&gt;tif_base, =
&amp;tif-&gt;tif_size))</DIV><DIV>=A0 =A0 =A0 =A0 =A0 =A0 =
tif-&gt;tif_flags &amp;=3D ~TIFF_MAPPED;</DIV><DIV>=A0 =A0 =A0 =A0 if =
(TIFFReadDirectory(tif)) {</DIV><DIV>=A0 =A0 =A0 =A0 =A0 =A0 =
tif-&gt;tif_rawcc =3D -1;</DIV><DIV>=A0 =A0 =A0 =A0 =A0 =A0 =
tif-&gt;tif_flags |=3D TIFF_BUFFERSETUP;</DIV><DIV>/*=A0=A0 =A0=A0 =A0=A0 =
=A0return (tif); STEVE - Remove this line */</DIV><DIV>=A0 =A0 =A0 =A0 =
}</DIV><DIV>/*=A0 =A0=A0 =A0break; STEVE - Remove this line =
*/</DIV><DIV>=A0 =A0 =A0=A0 =A0return (tif); /* STEVE - Add this line =
*/</DIV><DIV>=A0 =A0 case 'a':</DIV><DIV>=A0 =A0 =A0 =A0 /*</DIV><DIV>=A0 =
=A0 =A0 =A0=A0 * New directories are automatically append</DIV><DIV>=A0 =
=A0 =A0 =A0=A0 * to the end of the directory chain when they</DIV><DIV>=A0=
 =A0 =A0 =A0=A0 * are written out (see TIFFWriteDirectory).</DIV><DIV>=A0 =
=A0 =A0 =A0=A0 */</DIV><DIV>=A0 =A0 =A0 =A0 if =
(!TIFFDefaultDirectory(tif))</DIV><DIV>=A0 =A0 =A0 =A0 =A0 =A0 goto =
bad;</DIV><DIV>=A0 =A0 =A0 =A0 return (tif);</DIV><DIV>=A0 =A0 =
}</DIV><DIV><BR class=3D"khtml-block-placeholder"></DIV><DIV>This change =
allows TIFFOpen to still return a TIFF, and even though the first image =
is unreadable, the other pages are readable and it seems to work just =
fine. Would this change break anything?</DIV><DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV>You can find the corrupted =
TIFF file I've been testing with here:</DIV><DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV>&lt;<A =
href=3D"http://www.roydesign.net/corrupted.tif">http://www.roydesign.net/c=
orrupted.tif</A>&gt;</DIV><DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV>Steve</DIV><DIV><BR =
class=3D"khtml-block-placeholder"></DIV><DIV><DIV> <P style=3D"margin: =
0.0px 0.0px 0.0px 0.0px"><FONT face=3D"Helvetica" size=3D"3" =
style=3D"font: 12.0px Helvetica">--<SPAN =
class=3D"Apple-converted-space">=A0</SPAN></FONT></P> <P style=3D"margin: =
0.0px 0.0px 0.0px 0.0px"><FONT face=3D"Helvetica" size=3D"3" =
style=3D"font: 12.0px Helvetica">Steve Roy &lt;<A =
href=3D"mailto:sroy@mac.com">sroy@mac.com</A>&gt;</FONT></P> <P =
style=3D"margin: 0.0px 0.0px 0.0px 0.0px"><FONT face=3D"Helvetica" =
size=3D"3" style=3D"font: 12.0px Helvetica">Personal homepage: &lt;<A =
href=3D"http://homepage.mac.com/sroy">http://homepage.mac.com/sroy</A>&gt;=
</FONT></P> <P style=3D"margin: 0.0px 0.0px 0.0px 0.0px"><FONT =
face=3D"Helvetica" size=3D"3" style=3D"font: 12.0px Helvetica">Projects =
homepage: &lt;<A =
href=3D"http://www.roydesign.net">http://www.roydesign.net</A>&gt;</FONT><=
/P>  </DIV><BR></DIV></BODY></HTML>=

--Boundary_(ID_VwWRb8lTWFid+eXeXpOdrQ)--

--===============44830490520644284==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Tiff mailing list: Tiff@remotesensing.org
http://xserve.flids.com/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/

--===============44830490520644284==--
