/* $Id: tif_close.c,v 1.21 2016-01-23 21:20:34 erouault Exp $ */12/*3* Copyright (c) 1988-1997 Sam Leffler4* Copyright (c) 1991-1997 Silicon Graphics, Inc.5*6* Permission to use, copy, modify, distribute, and sell this software and7* its documentation for any purpose is hereby granted without fee, provided8* that (i) the above copyright notices and this permission notice appear in9* all copies of the software and related documentation, and (ii) the names of10* Sam Leffler and Silicon Graphics may not be used in any advertising or11* publicity relating to the software without the specific, prior written12* permission of Sam Leffler and Silicon Graphics.13*14* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,15* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY16* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.17*18* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR19* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,20* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,21* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF22* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE23* OF THIS SOFTWARE.24*/2526/*27* TIFF Library.28*/29#include "tiffiop.h"30#include <string.h>3132/************************************************************************/33/* TIFFCleanup() */34/************************************************************************/3536/**37* Auxiliary function to free the TIFF structure. Given structure will be38* completely freed, so you should save opened file handle and pointer39* to the close procedure in external variables before calling40* _TIFFCleanup(), if you will need these ones to close the file.41*42* @param tif A TIFF pointer.43*/4445void46TIFFCleanup(TIFF* tif)47{48/*49* Flush buffered data and directory (if dirty).50*/51if (tif->tif_mode != O_RDONLY)52TIFFFlush(tif);53(*tif->tif_cleanup)(tif);54TIFFFreeDirectory(tif);5556if (tif->tif_dirlist)57_TIFFfree(tif->tif_dirlist);5859/*60* Clean up client info links.61*/62while( tif->tif_clientinfo )63{64TIFFClientInfoLink *psLink = tif->tif_clientinfo;6566tif->tif_clientinfo = psLink->next;67_TIFFfree( psLink->name );68_TIFFfree( psLink );69}7071if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))72_TIFFfree(tif->tif_rawdata);73if (isMapped(tif))74TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);7576/*77* Clean up custom fields.78*/79if (tif->tif_fields && tif->tif_nfields > 0) {80uint32 i;8182for (i = 0; i < tif->tif_nfields; i++) {83TIFFField *fld = tif->tif_fields[i];84if (fld->field_bit == FIELD_CUSTOM &&85strncmp("Tag ", fld->field_name, 4) == 0) {86_TIFFfree(fld->field_name);87_TIFFfree(fld);88}89}9091_TIFFfree(tif->tif_fields);92}9394if (tif->tif_nfieldscompat > 0) {95uint32 i;9697for (i = 0; i < tif->tif_nfieldscompat; i++) {98if (tif->tif_fieldscompat[i].allocated_size)99_TIFFfree(tif->tif_fieldscompat[i].fields);100}101_TIFFfree(tif->tif_fieldscompat);102}103104_TIFFfree(tif);105}106107/************************************************************************/108/* TIFFClose() */109/************************************************************************/110111/**112* Close a previously opened TIFF file.113*114* TIFFClose closes a file that was previously opened with TIFFOpen().115* Any buffered data are flushed to the file, including the contents of116* the current directory (if modified); and all resources are reclaimed.117*118* @param tif A TIFF pointer.119*/120121void122TIFFClose(TIFF* tif)123{124TIFFCloseProc closeproc = tif->tif_closeproc;125thandle_t fd = tif->tif_clientdata;126127TIFFCleanup(tif);128(void) (*closeproc)(fd);129}130131/* vim: set ts=8 sts=8 sw=8 noet: */132133/*134* Local Variables:135* mode: c136* c-basic-offset: 8137* fill-column: 78138* End:139*/140141142