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