LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_close.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 43 49 87.8 %
Date: 2024-05-03 15:49:35 Functions: 3 3 100.0 %

          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       56457 : void TIFFCleanup(TIFF *tif)
      45             : {
      46             :     /*
      47             :      * Flush buffered data and directory (if dirty).
      48             :      */
      49       56457 :     if (tif->tif_mode != O_RDONLY)
      50       37510 :         TIFFFlush(tif);
      51       56456 :     (*tif->tif_cleanup)(tif);
      52       56458 :     TIFFFreeDirectory(tif);
      53             : 
      54       56455 :     _TIFFCleanupIFDOffsetAndNumberMaps(tif);
      55             : 
      56             :     /*
      57             :      * Clean up client info links.
      58             :      */
      59       56462 :     while (tif->tif_clientinfo)
      60             :     {
      61           0 :         TIFFClientInfoLink *psLink = tif->tif_clientinfo;
      62             : 
      63           0 :         tif->tif_clientinfo = psLink->next;
      64           0 :         _TIFFfreeExt(tif, psLink->name);
      65           0 :         _TIFFfreeExt(tif, psLink);
      66             :     }
      67             : 
      68       56462 :     if (tif->tif_rawdata && (tif->tif_flags & TIFF_MYBUFFER))
      69        6628 :         _TIFFfreeExt(tif, tif->tif_rawdata);
      70       56462 :     if (isMapped(tif))
      71           8 :         TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
      72             : 
      73             :     /*
      74             :      * Clean up custom fields.
      75             :      */
      76       56462 :     if (tif->tif_fields && tif->tif_nfields > 0)
      77             :     {
      78             :         uint32_t i;
      79             : 
      80     9382010 :         for (i = 0; i < tif->tif_nfields; i++)
      81             :         {
      82     9326320 :             TIFFField *fld = tif->tif_fields[i];
      83     9326320 :             if (fld->field_name != NULL)
      84             :             {
      85    15814400 :                 if (fld->field_bit == FIELD_CUSTOM &&
      86             :                     /* caution: tif_fields[i] must not be the beginning of a
      87             :                      * fields-array. Otherwise the following tags are also freed
      88             :                      * with the first free().
      89             :                      */
      90     6488720 :                     TIFFFieldIsAnonymous(fld))
      91             :                 {
      92           5 :                     _TIFFfreeExt(tif, fld->field_name);
      93           5 :                     _TIFFfreeExt(tif, fld);
      94             :                 }
      95             :             }
      96             :         }
      97             : 
      98       55687 :         _TIFFfreeExt(tif, tif->tif_fields);
      99             :     }
     100             : 
     101       56466 :     if (tif->tif_nfieldscompat > 0)
     102             :     {
     103             :         uint32_t i;
     104             : 
     105      169356 :         for (i = 0; i < tif->tif_nfieldscompat; i++)
     106             :         {
     107      112899 :             if (tif->tif_fieldscompat[i].allocated_size)
     108      112897 :                 _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
     109             :         }
     110       56457 :         _TIFFfreeExt(tif, tif->tif_fieldscompat);
     111             :     }
     112             : 
     113       56467 :     if (tif->tif_cur_cumulated_mem_alloc != 0)
     114             :     {
     115           0 :         TIFFErrorExtR(tif, "TIFFCleanup",
     116             :                       "tif_cur_cumulated_mem_alloc = %" PRIu64 " whereas it "
     117             :                       "should be 0",
     118           0 :                       (uint64_t)tif->tif_cur_cumulated_mem_alloc);
     119             :     }
     120             : 
     121       56467 :     _TIFFfreeExt(NULL, tif);
     122       56465 : }
     123             : 
     124             : /************************************************************************/
     125             : /*                    _TIFFCleanupIFDOffsetAndNumberMaps()              */
     126             : /************************************************************************/
     127             : 
     128       56502 : void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif)
     129             : {
     130       56502 :     if (tif->tif_map_dir_offset_to_number)
     131             :     {
     132       27762 :         TIFFHashSetDestroy(tif->tif_map_dir_offset_to_number);
     133       27770 :         tif->tif_map_dir_offset_to_number = NULL;
     134             :     }
     135       56510 :     if (tif->tif_map_dir_number_to_offset)
     136             :     {
     137       27766 :         TIFFHashSetDestroy(tif->tif_map_dir_number_to_offset);
     138       27773 :         tif->tif_map_dir_number_to_offset = NULL;
     139             :     }
     140       56517 : }
     141             : 
     142             : /************************************************************************/
     143             : /*                            TIFFClose()                               */
     144             : /************************************************************************/
     145             : 
     146             : /**
     147             :  * Close a previously opened TIFF file.
     148             :  *
     149             :  * TIFFClose closes a file that was previously opened with TIFFOpen().
     150             :  * Any buffered data are flushed to the file, including the contents of
     151             :  * the current directory (if modified); and all resources are reclaimed.
     152             :  *
     153             :  * @param tif A TIFF pointer.
     154             :  */
     155             : 
     156       56449 : void TIFFClose(TIFF *tif)
     157             : {
     158       56449 :     if (tif != NULL)
     159             :     {
     160       56449 :         TIFFCloseProc closeproc = tif->tif_closeproc;
     161       56449 :         thandle_t fd = tif->tif_clientdata;
     162             : 
     163       56449 :         TIFFCleanup(tif);
     164       56449 :         (void)(*closeproc)(fd);
     165             :     }
     166       56446 : }

Generated by: LCOV version 1.14