2005.06.17 19:49 "[Tiff] Fix for corrupted multipage TIFF", by Steve Roy

2005.06.17 19:49 "[Tiff] Fix for corrupted multipage TIFF", by Steve Roy

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>