AWARE [SYSTEMS] Imaging expertise for the Delphi developer
AWare Systems, Imaging expertise for the Delphi developer, Home TIFF and LibTiff Mailing List Archive

LibTiff Mailing List

TIFF and LibTiff Mailing List Archive
October 2005

Previous Thread
Next Thread

Previous by Thread
Next by Thread

Previous by Date
Next by Date

Contact

The TIFF Mailing List Homepage
This list is run by Frank Warmerdam
Archive maintained by AWare Systems



Valid HTML 4.01!



2005.10.26 16:34 "Fixed TIFFUnlinkDirectory()", by Eugene Shebeko

int TIFFUnlinkDirectory1(TIFF* tif, tdir_t dirn)
{
        static const char module[] = "TIFFUnlinkDirectory1";
        toff_t nextdir;
        toff_t off;
        tdir_t n;

        if (tif->tif_mode == O_RDONLY) {
                TIFFError(module, "Can not unlink directory in read-only file");
                return (0);
        }
        /*
         * Go to the directory before the one we want
         * to unlink and nab the offset of the link
         * field we'll need to patch.
         */
        nextdir = tif->tif_header.tiff_diroff;
        off = sizeof (uint16) + sizeof (uint16);
        for(n=0;n<dirn;n++) 
        {
                if (nextdir == 0)
                {
                        TIFFError(module, "Directory %d does not exist", dirn);
                        return (0);
                }
                if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
                        return (0);
        }
        /*
         * Advance to the directory to be unlinked and fetch
         * the offset of the directory that follows.
         */
        if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
                return (0);

        /*
         * If first directory change directory offset in header
         */
        if(dirn==0)tif->tif_header.tiff_diroff=nextdir;


        /*
         * Go back and patch the link field of the preceding
         * directory to point to the offset of the directory
         * that follows.
         */
        (void) TIFFSeekFile(tif, off, SEEK_SET);
        if (tif->tif_flags & TIFF_SWAB)TIFFSwabLong(&nextdir);
        if (!WriteOK(tif, &nextdir, sizeof (uint32))) {
                TIFFError(module, "Error writing directory link");
                return (0);
        }

        /*
         * Leave directory state setup safely.  We don't have
         * facilities for doing inserting and removing directories,
         * so it's safest to just invalidate everything.  This
         * means that the caller can only append to the directory
         * chain.
         */
        (*tif->tif_cleanup)(tif);
        if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
                _TIFFfree(tif->tif_rawdata);
                tif->tif_rawdata = NULL;
                tif->tif_rawcc = 0;
        }
        tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE);
        TIFFFreeDirectory(tif);
        TIFFDefaultDirectory(tif);
        tif->tif_diroff = 0;                    /* force link on next write */
        tif->tif_nextdiroff = 0;                /* next write must be at end */
        tif->tif_curoff = 0;
        tif->tif_row = (uint32) -1;
        tif->tif_curstrip = (tstrip_t) -1;

        return (1);
}