/*1* Copyright (c) 1988-1997 Sam Leffler2* Copyright (c) 1991-1997 Silicon Graphics, Inc.3*4* Permission to use, copy, modify, distribute, and sell this software and5* its documentation for any purpose is hereby granted without fee, provided6* that (i) the above copyright notices and this permission notice appear in7* all copies of the software and related documentation, and (ii) the names of8* Sam Leffler and Silicon Graphics may not be used in any advertising or9* publicity relating to the software without the specific, prior written10* permission of Sam Leffler and Silicon Graphics.11*12* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,13* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY14* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.15*16* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR17* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,18* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,19* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF20* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE21* OF THIS SOFTWARE.22*/2324/*25* TIFF Library.26*/27#include "tiffiop.h"2829int TIFFFlush(TIFF *tif)30{31if (tif->tif_mode == O_RDONLY)32return 1;3334if (!TIFFFlushData(tif))35return (0);3637/* In update (r+) mode we try to detect the case where38only the strip/tile map has been altered, and we try to39rewrite only that portion of the directory without40making any other changes */4142if ((tif->tif_flags & TIFF_DIRTYSTRIP) &&43!(tif->tif_flags & TIFF_DIRTYDIRECT) && tif->tif_mode == O_RDWR)44{45if (TIFFForceStrileArrayWriting(tif))46return 1;47}4849if ((tif->tif_flags & (TIFF_DIRTYDIRECT | TIFF_DIRTYSTRIP)) &&50!TIFFRewriteDirectory(tif))51return (0);5253return (1);54}5556/*57* This is an advanced writing function that must be used in a particular58* sequence, and together with TIFFDeferStrileArrayWriting(),59* to make its intended effect. Its aim is to force the writing of60* the [Strip/Tile][Offsets/ByteCounts] arrays at the end of the file, when61* they have not yet been rewritten.62*63* The typical sequence of calls is:64* TIFFOpen()65* [ TIFFCreateDirectory(tif) ]66* Set fields with calls to TIFFSetField(tif, ...)67* TIFFDeferStrileArrayWriting(tif)68* TIFFWriteCheck(tif, ...)69* TIFFWriteDirectory(tif)70* ... potentially create other directories and come back to the above directory71* TIFFForceStrileArrayWriting(tif)72*73* Returns 1 in case of success, 0 otherwise.74*/75int TIFFForceStrileArrayWriting(TIFF *tif)76{77static const char module[] = "TIFFForceStrileArrayWriting";78const int isTiled = TIFFIsTiled(tif);7980if (tif->tif_mode == O_RDONLY)81{82TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode");83return 0;84}85if (tif->tif_diroff == 0)86{87TIFFErrorExtR(tif, module, "Directory has not yet been written");88return 0;89}90if ((tif->tif_flags & TIFF_DIRTYDIRECT) != 0)91{92TIFFErrorExtR(tif, module,93"Directory has changes other than the strile arrays. "94"TIFFRewriteDirectory() should be called instead");95return 0;96}9798if (!(tif->tif_flags & TIFF_DIRTYSTRIP))99{100if (!(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&101tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&102tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&103tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&104tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&105tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&106tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&107tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0))108{109TIFFErrorExtR(tif, module,110"Function not called together with "111"TIFFDeferStrileArrayWriting()");112return 0;113}114115if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))116return 0;117}118119if (_TIFFRewriteField(tif,120isTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,121TIFF_LONG8, tif->tif_dir.td_nstrips,122tif->tif_dir.td_stripoffset_p) &&123_TIFFRewriteField(124tif, isTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,125TIFF_LONG8, tif->tif_dir.td_nstrips,126tif->tif_dir.td_stripbytecount_p))127{128tif->tif_flags &= ~TIFF_DIRTYSTRIP;129tif->tif_flags &= ~TIFF_BEENWRITING;130return 1;131}132133return 0;134}135136/*137* Flush buffered data to the file.138*139* Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING140* is not set, so that TIFFFlush() will proceed to write out the directory.141* The documentation says returning 1 is an error indicator, but not having142* been writing isn't exactly a an error. Hopefully this doesn't cause143* problems for other people.144*/145int TIFFFlushData(TIFF *tif)146{147if ((tif->tif_flags & TIFF_BEENWRITING) == 0)148return (1);149if (tif->tif_flags & TIFF_POSTENCODE)150{151tif->tif_flags &= ~TIFF_POSTENCODE;152if (!(*tif->tif_postencode)(tif))153return (0);154}155return (TIFFFlushData1(tif));156}157158159