/src/ghostpdl/tiff/libtiff/tif_flush.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
30 | | int |
31 | | TIFFFlush(TIFF* tif) |
32 | 2.19k | { |
33 | 2.19k | if( tif->tif_mode == O_RDONLY ) |
34 | 0 | return 1; |
35 | | |
36 | 2.19k | if (!TIFFFlushData(tif)) |
37 | 0 | return (0); |
38 | | |
39 | | /* In update (r+) mode we try to detect the case where |
40 | | only the strip/tile map has been altered, and we try to |
41 | | rewrite only that portion of the directory without |
42 | | making any other changes */ |
43 | | |
44 | 2.19k | if( (tif->tif_flags & TIFF_DIRTYSTRIP) |
45 | 2.19k | && !(tif->tif_flags & TIFF_DIRTYDIRECT) |
46 | 2.19k | && tif->tif_mode == O_RDWR ) |
47 | 0 | { |
48 | 0 | if( TIFFForceStrileArrayWriting(tif) ) |
49 | 0 | return 1; |
50 | 0 | } |
51 | | |
52 | 2.19k | if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP)) |
53 | 2.19k | && !TIFFRewriteDirectory(tif)) |
54 | 0 | return (0); |
55 | | |
56 | 2.19k | return (1); |
57 | 2.19k | } |
58 | | |
59 | | /* |
60 | | * This is an advanced writing function that must be used in a particular |
61 | | * sequence, and together with TIFFDeferStrileArrayWriting(), |
62 | | * to make its intended effect. Its aim is to force the writing of |
63 | | * the [Strip/Tile][Offsets/ByteCounts] arrays at the end of the file, when |
64 | | * they have not yet been rewritten. |
65 | | * |
66 | | * The typical sequence of calls is: |
67 | | * TIFFOpen() |
68 | | * [ TIFFCreateDirectory(tif) ] |
69 | | * Set fields with calls to TIFFSetField(tif, ...) |
70 | | * TIFFDeferStrileArrayWriting(tif) |
71 | | * TIFFWriteCheck(tif, ...) |
72 | | * TIFFWriteDirectory(tif) |
73 | | * ... potentially create other directories and come back to the above directory |
74 | | * TIFFForceStrileArrayWriting(tif) |
75 | | * |
76 | | * Returns 1 in case of success, 0 otherwise. |
77 | | */ |
78 | | int TIFFForceStrileArrayWriting(TIFF* tif) |
79 | 0 | { |
80 | 0 | static const char module[] = "TIFFForceStrileArrayWriting"; |
81 | 0 | const int isTiled = TIFFIsTiled(tif); |
82 | |
|
83 | 0 | if (tif->tif_mode == O_RDONLY) |
84 | 0 | { |
85 | 0 | TIFFErrorExt(tif->tif_clientdata, tif->tif_name, |
86 | 0 | "File opened in read-only mode"); |
87 | 0 | return 0; |
88 | 0 | } |
89 | 0 | if( tif->tif_diroff == 0 ) |
90 | 0 | { |
91 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
92 | 0 | "Directory has not yet been written"); |
93 | 0 | return 0; |
94 | 0 | } |
95 | 0 | if( (tif->tif_flags & TIFF_DIRTYDIRECT) != 0 ) |
96 | 0 | { |
97 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
98 | 0 | "Directory has changes other than the strile arrays. " |
99 | 0 | "TIFFRewriteDirectory() should be called instead"); |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | 0 | if( !(tif->tif_flags & TIFF_DIRTYSTRIP) ) |
104 | 0 | { |
105 | 0 | if( !(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && |
106 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && |
107 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && |
108 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && |
109 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && |
110 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && |
111 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && |
112 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) ) |
113 | 0 | { |
114 | 0 | TIFFErrorExt(tif->tif_clientdata, module, |
115 | 0 | "Function not called together with " |
116 | 0 | "TIFFDeferStrileArrayWriting()"); |
117 | 0 | return 0; |
118 | 0 | } |
119 | | |
120 | 0 | if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | 0 | if( _TIFFRewriteField( tif, |
125 | 0 | isTiled ? TIFFTAG_TILEOFFSETS : |
126 | 0 | TIFFTAG_STRIPOFFSETS, |
127 | 0 | TIFF_LONG8, |
128 | 0 | tif->tif_dir.td_nstrips, |
129 | 0 | tif->tif_dir.td_stripoffset_p ) |
130 | 0 | && _TIFFRewriteField( tif, |
131 | 0 | isTiled ? TIFFTAG_TILEBYTECOUNTS : |
132 | 0 | TIFFTAG_STRIPBYTECOUNTS, |
133 | 0 | TIFF_LONG8, |
134 | 0 | tif->tif_dir.td_nstrips, |
135 | 0 | tif->tif_dir.td_stripbytecount_p ) ) |
136 | 0 | { |
137 | 0 | tif->tif_flags &= ~TIFF_DIRTYSTRIP; |
138 | 0 | tif->tif_flags &= ~TIFF_BEENWRITING; |
139 | 0 | return 1; |
140 | 0 | } |
141 | | |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | | /* |
146 | | * Flush buffered data to the file. |
147 | | * |
148 | | * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING |
149 | | * is not set, so that TIFFFlush() will proceed to write out the directory. |
150 | | * The documentation says returning 1 is an error indicator, but not having |
151 | | * been writing isn't exactly a an error. Hopefully this doesn't cause |
152 | | * problems for other people. |
153 | | */ |
154 | | int |
155 | | TIFFFlushData(TIFF* tif) |
156 | 52.7k | { |
157 | 52.7k | if ((tif->tif_flags & TIFF_BEENWRITING) == 0) |
158 | 2.19k | return (1); |
159 | 50.6k | if (tif->tif_flags & TIFF_POSTENCODE) { |
160 | 48.6k | tif->tif_flags &= ~TIFF_POSTENCODE; |
161 | 48.6k | if (!(*tif->tif_postencode)(tif)) |
162 | 0 | return (0); |
163 | 48.6k | } |
164 | 50.6k | return (TIFFFlushData1(tif)); |
165 | 50.6k | } |
166 | | |
167 | | /* vim: set ts=8 sts=8 sw=8 noet: */ |
168 | | /* |
169 | | * Local Variables: |
170 | | * mode: c |
171 | | * c-basic-offset: 8 |
172 | | * fill-column: 78 |
173 | | * End: |
174 | | */ |