1 /* PDFlib GmbH cvsid:
2  * $Id: tif_close.c,v 1.12.2.1 2006/12/27 15:03:32 rjs Exp $ */
3 
4 /*
5  * Copyright (c) 1988-1997 Sam Leffler
6  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and
9  * its documentation for any purpose is hereby granted without fee, provided
10  * that (i) the above copyright notices and this permission notice appear in
11  * all copies of the software and related documentation, and (ii) the names of
12  * Sam Leffler and Silicon Graphics may not be used in any advertising or
13  * publicity relating to the software without the specific, prior written
14  * permission of Sam Leffler and Silicon Graphics.
15  *
16  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
21  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25  * OF THIS SOFTWARE.
26  */
27 
28 /*
29  * TIFF Library.
30  */
31 #include "tiffiop.h"
32 
33 /************************************************************************/
34 /*                            TIFFCleanup()                             */
35 /************************************************************************/
36 
37 /**
38  * Auxiliary function to free the TIFF structure. Given structure will be
39  * completetly freed, so you should save opened file handle and pointer
40  * to the close procedure in external variables before calling
41  * _TIFFCleanup(), if you will need these ones to close the file.
42  *
43  * @param tif A TIFF pointer.
44  */
45 
46 void
TIFFCleanup(TIFF * tif)47 TIFFCleanup(TIFF* tif)
48 {
49 #ifdef PDFLIB_TIFFWRITE_SUPPORT
50 	if (tif->tif_mode != O_RDONLY)
51 	    /*
52 	     * Flush buffered data and directory (if dirty).
53 	     */
54 	    TIFFFlush(tif);
55 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
56 	(*tif->tif_cleanup)(tif);
57 	TIFFFreeDirectory(tif);
58 
59 	if (tif->tif_dirlist)
60 	    _TIFFfree(tif->tif_dirlist);
61 
62 	/* Clean up client info links */
63 	while( tif->tif_clientinfo )
64 	{
65 	    TIFFClientInfoLink *llink = tif->tif_clientinfo;
66 
67 	    tif->tif_clientinfo = llink->next;
68 	    _TIFFfree(llink->name );
69 	    _TIFFfree(llink );
70 	}
71 
72 	if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
73 	    _TIFFfree(tif->tif_rawdata);
74 	if (isMapped(tif))
75 	    TIFFUnmapFileContents(tif, tif->tif_base, tif->tif_size);
76 
77 	/* Clean up custom fields */
78 	if (tif->tif_nfields > 0)
79 	{
80 	    unsigned int  i;
81 
82 	    for (i = 0; i < tif->tif_nfields; i++)
83 	    {
84 		TIFFFieldInfo *fld = tif->tif_fieldinfo[i];
85 		if (fld->field_bit == FIELD_CUSTOM &&
86 		    strncmp("Tag ", fld->field_name, 4) == 0)
87 		{
88 		    _TIFFfree(fld->field_name);
89 		    _TIFFfree(fld);
90 		}
91 	    }
92 
93 	    _TIFFfree(tif->tif_fieldinfo);
94 	}
95 
96 	_TIFFfree(tif);
97 }
98 
99 /************************************************************************/
100 /*                            TIFFClose()                               */
101 /************************************************************************/
102 
103 /**
104  * Close a previously opened TIFF file.
105  *
106  * TIFFClose closes a file that was previously opened with TIFFOpen().
107  * Any buffered data are flushed to the file, including the contents of
108  * the current directory (if modified); and all resources are reclaimed.
109  *
110  * @param tif A TIFF pointer.
111  */
112 
113 void
TIFFClose(TIFF * tif)114 TIFFClose(TIFF* tif)
115 {
116 	TIFFCloseProc closeproc = tif->tif_closeproc;
117 	FILE * fd = (FILE *)tif->tif_clientdata;
118 
119 	TIFFCleanup(tif);
120 	(void) (*closeproc)(fd);
121 }
122 
123