Line data Source code
1 : /* 2 : * Copyright (c) 1988-1997 Sam Leffler 3 : * Copyright (c) 1991-1997 Silicon Graphics, Inc. 4 : * 5 : * Permission to use, copy, modify, distribute, and sell this software and 6 : * its documentation for any purpose is hereby granted without fee, provided 7 : * that (i) the above copyright notices and this permission notice appear in 8 : * all copies of the software and related documentation, and (ii) the names of 9 : * Sam Leffler and Silicon Graphics may not be used in any advertising or 10 : * publicity relating to the software without the specific, prior written 11 : * permission of Sam Leffler and Silicon Graphics. 12 : * 13 : * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 14 : * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 15 : * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 16 : * 17 : * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR 18 : * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 19 : * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 20 : * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 21 : * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 22 : * OF THIS SOFTWARE. 23 : */ 24 : 25 : /* 26 : * TIFF Library. 27 : */ 28 : #include "tiffiop.h" 29 : #include <string.h> 30 : 31 : /************************************************************************/ 32 : /* TIFFCleanup() */ 33 : /************************************************************************/ 34 : 35 : /** 36 : * Auxiliary function to free the TIFF structure. Given structure will be 37 : * completely freed, so you should save opened file handle and pointer 38 : * to the close procedure in external variables before calling 39 : * _TIFFCleanup(), if you will need these ones to close the file. 40 : * 41 : * @param tif A TIFF pointer. 42 : */ 43 : 44 68761 : void TIFFCleanup(TIFF *tif) 45 : { 46 : /* 47 : * Flush buffered data and directory (if dirty). 48 : */ 49 68761 : if (tif->tif_mode != O_RDONLY) 50 42535 : TIFFFlush(tif); 51 68760 : TIFFFreeDirectory(tif); 52 : 53 68762 : _TIFFCleanupIFDOffsetAndNumberMaps(tif); 54 : 55 : /* 56 : * Clean up client info links. 57 : */ 58 68762 : while (tif->tif_clientinfo) 59 : { 60 0 : TIFFClientInfoLink *psLink = tif->tif_clientinfo; 61 : 62 0 : tif->tif_clientinfo = psLink->next; 63 0 : _TIFFfreeExt(tif, psLink->name); 64 0 : _TIFFfreeExt(tif, psLink); 65 : } 66 : 67 68762 : if (tif->tif_rawdata && (tif->tif_flags & TIFF_MYBUFFER)) 68 10191 : _TIFFfreeExt(tif, tif->tif_rawdata); 69 68762 : if (isMapped(tif)) 70 8 : TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size); 71 : 72 : /* 73 : * Clean up custom fields. 74 : */ 75 68762 : if (tif->tif_fields && tif->tif_nfields > 0) 76 : { 77 : uint32_t i; 78 : 79 11423400 : for (i = 0; i < tif->tif_nfields; i++) 80 : { 81 11356700 : TIFFField *fld = tif->tif_fields[i]; 82 11356700 : if (fld->field_name != NULL) 83 : { 84 19259200 : if (fld->field_bit == FIELD_CUSTOM && 85 : /* caution: tif_fields[i] must not be the beginning of a 86 : * fields-array. Otherwise the following tags are also freed 87 : * with the first free(). 88 : */ 89 7903160 : TIFFFieldIsAnonymous(fld)) 90 : { 91 5 : _TIFFfreeExt(tif, fld->field_name); 92 5 : _TIFFfreeExt(tif, fld); 93 : } 94 : } 95 : } 96 : 97 66641 : _TIFFfreeExt(tif, tif->tif_fields); 98 : } 99 : 100 68764 : if (tif->tif_nfieldscompat > 0) 101 : { 102 : uint32_t i; 103 : 104 206247 : for (i = 0; i < tif->tif_nfieldscompat; i++) 105 : { 106 137497 : if (tif->tif_fieldscompat[i].allocated_size) 107 137497 : _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields); 108 : } 109 68750 : _TIFFfreeExt(tif, tif->tif_fieldscompat); 110 : } 111 : 112 68763 : if (tif->tif_cur_cumulated_mem_alloc != 0) 113 : { 114 0 : TIFFErrorExtR(tif, "TIFFCleanup", 115 : "tif_cur_cumulated_mem_alloc = %" PRIu64 " whereas it " 116 : "should be 0", 117 0 : (uint64_t)tif->tif_cur_cumulated_mem_alloc); 118 : } 119 : 120 68763 : _TIFFfreeExt(NULL, tif); 121 68762 : } 122 : 123 : /************************************************************************/ 124 : /* _TIFFCleanupIFDOffsetAndNumberMaps() */ 125 : /************************************************************************/ 126 : 127 68825 : void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif) 128 : { 129 68825 : if (tif->tif_map_dir_offset_to_number) 130 : { 131 68820 : TIFFHashSetDestroy(tif->tif_map_dir_offset_to_number); 132 68822 : tif->tif_map_dir_offset_to_number = NULL; 133 : } 134 68827 : if (tif->tif_map_dir_number_to_offset) 135 : { 136 68821 : TIFFHashSetDestroy(tif->tif_map_dir_number_to_offset); 137 68821 : tif->tif_map_dir_number_to_offset = NULL; 138 : } 139 68827 : } 140 : 141 : /************************************************************************/ 142 : /* TIFFClose() */ 143 : /************************************************************************/ 144 : 145 : /** 146 : * Close a previously opened TIFF file. 147 : * 148 : * TIFFClose closes a file that was previously opened with TIFFOpen(). 149 : * Any buffered data are flushed to the file, including the contents of 150 : * the current directory (if modified); and all resources are reclaimed. 151 : * 152 : * @param tif A TIFF pointer. 153 : */ 154 : 155 68744 : void TIFFClose(TIFF *tif) 156 : { 157 68744 : if (tif != NULL) 158 : { 159 68744 : TIFFCloseProc closeproc = tif->tif_closeproc; 160 68744 : thandle_t fd = tif->tif_clientdata; 161 : 162 68744 : TIFFCleanup(tif); 163 68747 : (void)(*closeproc)(fd); 164 : } 165 68746 : }