tif_close.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * TIFF Library.
  26. */
  27. #include "tiffiop.h"
  28. #include <string.h>
  29. /************************************************************************/
  30. /* TIFFCleanup() */
  31. /************************************************************************/
  32. /**
  33. * Auxiliary function to free the TIFF structure. Given structure will be
  34. * completely freed, so you should save opened file handle and pointer
  35. * to the close procedure in external variables before calling
  36. * _TIFFCleanup(), if you will need these ones to close the file.
  37. *
  38. * @param tif A TIFF pointer.
  39. */
  40. void TIFFCleanup(TIFF *tif)
  41. {
  42. /*
  43. * Flush buffered data and directory (if dirty).
  44. */
  45. if (tif->tif_mode != O_RDONLY)
  46. TIFFFlush(tif);
  47. TIFFFreeDirectory(tif);
  48. _TIFFCleanupIFDOffsetAndNumberMaps(tif);
  49. /*
  50. * Clean up client info links.
  51. */
  52. while (tif->tif_clientinfo)
  53. {
  54. TIFFClientInfoLink *psLink = tif->tif_clientinfo;
  55. tif->tif_clientinfo = psLink->next;
  56. _TIFFfreeExt(tif, psLink->name);
  57. _TIFFfreeExt(tif, psLink);
  58. }
  59. if (tif->tif_rawdata && (tif->tif_flags & TIFF_MYBUFFER))
  60. _TIFFfreeExt(tif, tif->tif_rawdata);
  61. if (isMapped(tif))
  62. TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
  63. /*
  64. * Clean up custom fields.
  65. */
  66. if (tif->tif_fields && tif->tif_nfields > 0)
  67. {
  68. uint32_t i;
  69. for (i = 0; i < tif->tif_nfields; i++)
  70. {
  71. TIFFField *fld = tif->tif_fields[i];
  72. if (fld->field_name != NULL)
  73. {
  74. if (fld->field_bit == FIELD_CUSTOM &&
  75. /* caution: tif_fields[i] must not be the beginning of a
  76. * fields-array. Otherwise the following tags are also freed
  77. * with the first free().
  78. */
  79. TIFFFieldIsAnonymous(fld))
  80. {
  81. _TIFFfreeExt(tif, fld->field_name);
  82. _TIFFfreeExt(tif, fld);
  83. }
  84. }
  85. }
  86. _TIFFfreeExt(tif, tif->tif_fields);
  87. }
  88. if (tif->tif_nfieldscompat > 0)
  89. {
  90. uint32_t i;
  91. for (i = 0; i < tif->tif_nfieldscompat; i++)
  92. {
  93. if (tif->tif_fieldscompat[i].allocated_size)
  94. _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
  95. }
  96. _TIFFfreeExt(tif, tif->tif_fieldscompat);
  97. }
  98. if (tif->tif_cur_cumulated_mem_alloc != 0)
  99. {
  100. TIFFErrorExtR(tif, "TIFFCleanup",
  101. "tif_cur_cumulated_mem_alloc = %" PRIu64 " whereas it "
  102. "should be 0",
  103. (uint64_t)tif->tif_cur_cumulated_mem_alloc);
  104. }
  105. _TIFFfreeExt(NULL, tif);
  106. }
  107. /************************************************************************/
  108. /* _TIFFCleanupIFDOffsetAndNumberMaps() */
  109. /************************************************************************/
  110. void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif)
  111. {
  112. if (tif->tif_map_dir_offset_to_number)
  113. {
  114. TIFFHashSetDestroy(tif->tif_map_dir_offset_to_number);
  115. tif->tif_map_dir_offset_to_number = NULL;
  116. }
  117. if (tif->tif_map_dir_number_to_offset)
  118. {
  119. TIFFHashSetDestroy(tif->tif_map_dir_number_to_offset);
  120. tif->tif_map_dir_number_to_offset = NULL;
  121. }
  122. }
  123. /************************************************************************/
  124. /* TIFFClose() */
  125. /************************************************************************/
  126. /**
  127. * Close a previously opened TIFF file.
  128. *
  129. * TIFFClose closes a file that was previously opened with TIFFOpen().
  130. * Any buffered data are flushed to the file, including the contents of
  131. * the current directory (if modified); and all resources are reclaimed.
  132. *
  133. * @param tif A TIFF pointer.
  134. */
  135. void TIFFClose(TIFF *tif)
  136. {
  137. if (tif != NULL)
  138. {
  139. TIFFCloseProc closeproc = tif->tif_closeproc;
  140. thandle_t fd = tif->tif_clientdata;
  141. TIFFCleanup(tif);
  142. (void)(*closeproc)(fd);
  143. }
  144. }