tif_close.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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
  41. TIFFCleanup(TIFF* tif)
  42. {
  43. /*
  44. * Flush buffered data and directory (if dirty).
  45. */
  46. if (tif->tif_mode != O_RDONLY)
  47. TIFFFlush(tif);
  48. (*tif->tif_cleanup)(tif);
  49. TIFFFreeDirectory(tif);
  50. if (tif->tif_dirlist)
  51. _TIFFfree(tif->tif_dirlist);
  52. /*
  53. * Clean up client info links.
  54. */
  55. while( tif->tif_clientinfo )
  56. {
  57. TIFFClientInfoLink *psLink = tif->tif_clientinfo;
  58. tif->tif_clientinfo = psLink->next;
  59. _TIFFfree( psLink->name );
  60. _TIFFfree( psLink );
  61. }
  62. if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
  63. _TIFFfree(tif->tif_rawdata);
  64. if (isMapped(tif))
  65. TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
  66. /*
  67. * Clean up custom fields.
  68. */
  69. if (tif->tif_fields && tif->tif_nfields > 0) {
  70. uint32_t i;
  71. for (i = 0; i < tif->tif_nfields; i++) {
  72. TIFFField *fld = tif->tif_fields[i];
  73. if (fld->field_bit == FIELD_CUSTOM &&
  74. strncmp("Tag ", fld->field_name, 4) == 0) {
  75. _TIFFfree(fld->field_name);
  76. _TIFFfree(fld);
  77. }
  78. }
  79. _TIFFfree(tif->tif_fields);
  80. }
  81. if (tif->tif_nfieldscompat > 0) {
  82. uint32_t i;
  83. for (i = 0; i < tif->tif_nfieldscompat; i++) {
  84. if (tif->tif_fieldscompat[i].allocated_size)
  85. _TIFFfree(tif->tif_fieldscompat[i].fields);
  86. }
  87. _TIFFfree(tif->tif_fieldscompat);
  88. }
  89. _TIFFfree(tif);
  90. }
  91. /************************************************************************/
  92. /* TIFFClose() */
  93. /************************************************************************/
  94. /**
  95. * Close a previously opened TIFF file.
  96. *
  97. * TIFFClose closes a file that was previously opened with TIFFOpen().
  98. * Any buffered data are flushed to the file, including the contents of
  99. * the current directory (if modified); and all resources are reclaimed.
  100. *
  101. * @param tif A TIFF pointer.
  102. */
  103. void
  104. TIFFClose(TIFF* tif)
  105. {
  106. TIFFCloseProc closeproc = tif->tif_closeproc;
  107. thandle_t fd = tif->tif_clientdata;
  108. TIFFCleanup(tif);
  109. (void) (*closeproc)(fd);
  110. }
  111. /* vim: set ts=8 sts=8 sw=8 noet: */
  112. /*
  113. * Local Variables:
  114. * mode: c
  115. * c-basic-offset: 8
  116. * fill-column: 78
  117. * End:
  118. */