tif_close.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. (*tif->tif_cleanup)(tif);
  48. TIFFFreeDirectory(tif);
  49. _TIFFCleanupIFDOffsetAndNumberMaps(tif);
  50. /*
  51. * Clean up client info links.
  52. */
  53. while (tif->tif_clientinfo)
  54. {
  55. TIFFClientInfoLink *psLink = tif->tif_clientinfo;
  56. tif->tif_clientinfo = psLink->next;
  57. _TIFFfreeExt(tif, psLink->name);
  58. _TIFFfreeExt(tif, psLink);
  59. }
  60. if (tif->tif_rawdata && (tif->tif_flags & TIFF_MYBUFFER))
  61. _TIFFfreeExt(tif, tif->tif_rawdata);
  62. if (isMapped(tif))
  63. TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
  64. /*
  65. * Clean up custom fields.
  66. */
  67. if (tif->tif_fields && tif->tif_nfields > 0)
  68. {
  69. uint32_t i;
  70. for (i = 0; i < tif->tif_nfields; i++)
  71. {
  72. TIFFField *fld = tif->tif_fields[i];
  73. if (fld->field_name != NULL)
  74. {
  75. if (fld->field_bit == FIELD_CUSTOM &&
  76. /* caution: tif_fields[i] must not be the beginning of a
  77. * fields-array. Otherwise the following tags are also freed
  78. * with the first free().
  79. */
  80. TIFFFieldIsAnonymous(fld))
  81. {
  82. _TIFFfreeExt(tif, fld->field_name);
  83. _TIFFfreeExt(tif, fld);
  84. }
  85. }
  86. }
  87. _TIFFfreeExt(tif, tif->tif_fields);
  88. }
  89. if (tif->tif_nfieldscompat > 0)
  90. {
  91. uint32_t i;
  92. for (i = 0; i < tif->tif_nfieldscompat; i++)
  93. {
  94. if (tif->tif_fieldscompat[i].allocated_size)
  95. _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
  96. }
  97. _TIFFfreeExt(tif, tif->tif_fieldscompat);
  98. }
  99. _TIFFfreeExt(NULL, tif);
  100. }
  101. /************************************************************************/
  102. /* _TIFFCleanupIFDOffsetAndNumberMaps() */
  103. /************************************************************************/
  104. void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif)
  105. {
  106. if (tif->tif_map_dir_offset_to_number)
  107. {
  108. TIFFHashSetDestroy(tif->tif_map_dir_offset_to_number);
  109. tif->tif_map_dir_offset_to_number = NULL;
  110. }
  111. if (tif->tif_map_dir_number_to_offset)
  112. {
  113. TIFFHashSetDestroy(tif->tif_map_dir_number_to_offset);
  114. tif->tif_map_dir_number_to_offset = NULL;
  115. }
  116. }
  117. /************************************************************************/
  118. /* TIFFClose() */
  119. /************************************************************************/
  120. /**
  121. * Close a previously opened TIFF file.
  122. *
  123. * TIFFClose closes a file that was previously opened with TIFFOpen().
  124. * Any buffered data are flushed to the file, including the contents of
  125. * the current directory (if modified); and all resources are reclaimed.
  126. *
  127. * @param tif A TIFF pointer.
  128. */
  129. void TIFFClose(TIFF *tif)
  130. {
  131. if (tif != NULL)
  132. {
  133. TIFFCloseProc closeproc = tif->tif_closeproc;
  134. thandle_t fd = tif->tif_clientdata;
  135. TIFFCleanup(tif);
  136. (void)(*closeproc)(fd);
  137. }
  138. }