2005.10.26 16:34 "[Tiff] Fixed TIFFUnlinkDirectory()", by Шебеко Евгений

2005.10.26 16:34 "[Tiff] Fixed TIFFUnlinkDirectory()", by Шебеко Евгений

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);
}