xref: /reactos/dll/3rdparty/libtiff/tif_dirwrite.c (revision bd712186)
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  * Directory Write Support Routines.
29  */
30 #include <precomp.h>
31 #include <float.h>
32 
33 #ifdef HAVE_IEEEFP
34 #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
35 #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
36 #else
37 extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32 n, float* fp);
38 extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
39 #endif
40 
41 static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
42 
43 static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
44 #if 0
45 static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
46 #endif
47 
48 static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
49 static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
50 #ifdef notdef
51 static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
52 #endif
53 static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
54 #if 0
55 static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
56 #endif
57 #ifdef notdef
58 static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
59 #endif
60 static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
61 #if 0
62 static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
63 #endif
64 static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
65 static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
66 static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
67 #ifdef notdef
68 static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
69 #endif
70 static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
71 #if 0
72 static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
73 #endif
74 static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
75 static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
76 #if 0
77 static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
78 #endif
79 #ifdef notdef
80 static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
81 #endif
82 static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
83 #if 0
84 static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
85 #endif
86 #ifdef notdef
87 static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
88 #endif
89 static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
90 #ifdef notdef
91 static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
92 #endif
93 static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
94 static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
95 static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
96 static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
97 #ifdef notdef
98 static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
99 #endif
100 static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
101 #if 0
102 static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
103 #endif
104 #ifdef notdef
105 static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
106 #endif
107 static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
108 #if 0
109 static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
110 #endif
111 static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
112 #ifdef notdef
113 static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
114 #endif
115 static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
116 static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
117 static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
118 #ifdef notdef
119 static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
120 #endif
121 static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
122 static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
123 static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
124 
125 static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
126 static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
127 #ifdef notdef
128 static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
129 #endif
130 static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
131 #ifdef notdef
132 static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
133 #endif
134 static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
135 static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
136 static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
137 #ifdef notdef
138 static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
139 #endif
140 static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
141 static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
142 static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
143 #ifdef notdef
144 static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
145 #endif
146 static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
147 #ifdef notdef
148 static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
149 #endif
150 static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
151 #ifdef notdef
152 static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
153 #endif
154 static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
155 static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
156 static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
157 static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
158 #ifdef notdef
159 static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
160 #endif
161 static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
162 #ifdef notdef
163 static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
164 #endif
165 static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
166 static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
167 static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
168 
169 static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
170 
171 static int TIFFLinkDirectory(TIFF*);
172 
173 /*
174  * Write the contents of the current directory
175  * to the specified file.  This routine doesn't
176  * handle overwriting a directory with auxiliary
177  * storage that's been changed.
178  */
179 int
180 TIFFWriteDirectory(TIFF* tif)
181 {
182 	return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
183 }
184 
185 /*
186  * This is an advanced writing function that must be used in a particular
187  * sequence, and generally together with TIFFForceStrileArrayWriting(),
188  * to make its intended effect. Its aim is to modify the location
189  * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file.
190  * More precisely, when TIFFWriteCheck() will be called, the tag entries for
191  * those arrays will be written with type = count = offset = 0 as a temporary
192  * value.
193  *
194  * Its effect is only valid for the current directory, and before
195  * TIFFWriteDirectory() is first called, and  will be reset when
196  * changing directory.
197  *
198  * The typical sequence of calls is:
199  * TIFFOpen()
200  * [ TIFFCreateDirectory(tif) ]
201  * Set fields with calls to TIFFSetField(tif, ...)
202  * TIFFDeferStrileArrayWriting(tif)
203  * TIFFWriteCheck(tif, ...)
204  * TIFFWriteDirectory(tif)
205  * ... potentially create other directories and come back to the above directory
206  * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file
207  *
208  * Returns 1 in case of success, 0 otherwise.
209  */
210 int TIFFDeferStrileArrayWriting(TIFF* tif)
211 {
212     static const char module[] = "TIFFDeferStrileArrayWriting";
213     if (tif->tif_mode == O_RDONLY)
214     {
215         TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
216                      "File opened in read-only mode");
217         return 0;
218     }
219     if( tif->tif_diroff != 0 )
220     {
221         TIFFErrorExt(tif->tif_clientdata, module,
222                      "Directory has already been written");
223         return 0;
224     }
225 
226     tif->tif_dir.td_deferstrilearraywriting = TRUE;
227     return 1;
228 }
229 
230 /*
231  * Similar to TIFFWriteDirectory(), writes the directory out
232  * but leaves all data structures in memory so that it can be
233  * written again.  This will make a partially written TIFF file
234  * readable before it is successfully completed/closed.
235  */
236 int
237 TIFFCheckpointDirectory(TIFF* tif)
238 {
239 	int rc;
240 	/* Setup the strips arrays, if they haven't already been. */
241 	if (tif->tif_dir.td_stripoffset_p == NULL)
242 	    (void) TIFFSetupStrips(tif);
243 	rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
244 	(void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
245 	return rc;
246 }
247 
248 int
249 TIFFWriteCustomDirectory(TIFF* tif, uint64* pdiroff)
250 {
251 	return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
252 }
253 
254 /*
255  * Similar to TIFFWriteDirectory(), but if the directory has already
256  * been written once, it is relocated to the end of the file, in case it
257  * has changed in size.  Note that this will result in the loss of the
258  * previously used directory space.
259  */
260 int
261 TIFFRewriteDirectory( TIFF *tif )
262 {
263 	static const char module[] = "TIFFRewriteDirectory";
264 
265 	/* We don't need to do anything special if it hasn't been written. */
266 	if( tif->tif_diroff == 0 )
267 		return TIFFWriteDirectory( tif );
268 
269 	/*
270 	 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
271 	 * will cause it to be added after this directories current pre-link.
272 	 */
273 
274 	if (!(tif->tif_flags&TIFF_BIGTIFF))
275 	{
276 		if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
277 		{
278 			tif->tif_header.classic.tiff_diroff = 0;
279 			tif->tif_diroff = 0;
280 
281 			TIFFSeekFile(tif,4,SEEK_SET);
282 			if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
283 			{
284 				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
285 				    "Error updating TIFF header");
286 				return (0);
287 			}
288 		}
289 		else
290 		{
291 			uint32 nextdir;
292 			nextdir = tif->tif_header.classic.tiff_diroff;
293 			while(1) {
294 				uint16 dircount;
295 				uint32 nextnextdir;
296 
297 				if (!SeekOK(tif, nextdir) ||
298 				    !ReadOK(tif, &dircount, 2)) {
299 					TIFFErrorExt(tif->tif_clientdata, module,
300 					     "Error fetching directory count");
301 					return (0);
302 				}
303 				if (tif->tif_flags & TIFF_SWAB)
304 					TIFFSwabShort(&dircount);
305 				(void) TIFFSeekFile(tif,
306 				    nextdir+2+dircount*12, SEEK_SET);
307 				if (!ReadOK(tif, &nextnextdir, 4)) {
308 					TIFFErrorExt(tif->tif_clientdata, module,
309 					     "Error fetching directory link");
310 					return (0);
311 				}
312 				if (tif->tif_flags & TIFF_SWAB)
313 					TIFFSwabLong(&nextnextdir);
314 				if (nextnextdir==tif->tif_diroff)
315 				{
316 					uint32 m;
317 					m=0;
318 					(void) TIFFSeekFile(tif,
319 					    nextdir+2+dircount*12, SEEK_SET);
320 					if (!WriteOK(tif, &m, 4)) {
321 						TIFFErrorExt(tif->tif_clientdata, module,
322 						     "Error writing directory link");
323 						return (0);
324 					}
325 					tif->tif_diroff=0;
326 					break;
327 				}
328 				nextdir=nextnextdir;
329 			}
330 		}
331 	}
332 	else
333 	{
334 		if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
335 		{
336 			tif->tif_header.big.tiff_diroff = 0;
337 			tif->tif_diroff = 0;
338 
339 			TIFFSeekFile(tif,8,SEEK_SET);
340 			if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
341 			{
342 				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
343 				    "Error updating TIFF header");
344 				return (0);
345 			}
346 		}
347 		else
348 		{
349 			uint64 nextdir;
350 			nextdir = tif->tif_header.big.tiff_diroff;
351 			while(1) {
352 				uint64 dircount64;
353 				uint16 dircount;
354 				uint64 nextnextdir;
355 
356 				if (!SeekOK(tif, nextdir) ||
357 				    !ReadOK(tif, &dircount64, 8)) {
358 					TIFFErrorExt(tif->tif_clientdata, module,
359 					     "Error fetching directory count");
360 					return (0);
361 				}
362 				if (tif->tif_flags & TIFF_SWAB)
363 					TIFFSwabLong8(&dircount64);
364 				if (dircount64>0xFFFF)
365 				{
366 					TIFFErrorExt(tif->tif_clientdata, module,
367 					     "Sanity check on tag count failed, likely corrupt TIFF");
368 					return (0);
369 				}
370 				dircount=(uint16)dircount64;
371 				(void) TIFFSeekFile(tif,
372 				    nextdir+8+dircount*20, SEEK_SET);
373 				if (!ReadOK(tif, &nextnextdir, 8)) {
374 					TIFFErrorExt(tif->tif_clientdata, module,
375 					     "Error fetching directory link");
376 					return (0);
377 				}
378 				if (tif->tif_flags & TIFF_SWAB)
379 					TIFFSwabLong8(&nextnextdir);
380 				if (nextnextdir==tif->tif_diroff)
381 				{
382 					uint64 m;
383 					m=0;
384 					(void) TIFFSeekFile(tif,
385 					    nextdir+8+dircount*20, SEEK_SET);
386 					if (!WriteOK(tif, &m, 8)) {
387 						TIFFErrorExt(tif->tif_clientdata, module,
388 						     "Error writing directory link");
389 						return (0);
390 					}
391 					tif->tif_diroff=0;
392 					break;
393 				}
394 				nextdir=nextnextdir;
395 			}
396 		}
397 	}
398 
399 	/*
400 	 * Now use TIFFWriteDirectory() normally.
401 	 */
402 
403 	return TIFFWriteDirectory( tif );
404 }
405 
406 static int
407 TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
408 {
409 	static const char module[] = "TIFFWriteDirectorySec";
410 	uint32 ndir;
411 	TIFFDirEntry* dir;
412 	uint32 dirsize;
413 	void* dirmem;
414 	uint32 m;
415 	if (tif->tif_mode == O_RDONLY)
416 		return (1);
417 
418         _TIFFFillStriles( tif );
419 
420 	/*
421 	 * Clear write state so that subsequent images with
422 	 * different characteristics get the right buffers
423 	 * setup for them.
424 	 */
425 	if (imagedone)
426 	{
427 		if (tif->tif_flags & TIFF_POSTENCODE)
428 		{
429 			tif->tif_flags &= ~TIFF_POSTENCODE;
430 			if (!(*tif->tif_postencode)(tif))
431 			{
432 				TIFFErrorExt(tif->tif_clientdata,module,
433 				    "Error post-encoding before directory write");
434 				return (0);
435 			}
436 		}
437 		(*tif->tif_close)(tif);       /* shutdown encoder */
438 		/*
439 		 * Flush any data that might have been written
440 		 * by the compression close+cleanup routines.  But
441                  * be careful not to write stuff if we didn't add data
442                  * in the previous steps as the "rawcc" data may well be
443                  * a previously read tile/strip in mixed read/write mode.
444 		 */
445 		if (tif->tif_rawcc > 0
446 		    && (tif->tif_flags & TIFF_BEENWRITING) != 0 )
447 		{
448 		    if( !TIFFFlushData1(tif) )
449                     {
450 			TIFFErrorExt(tif->tif_clientdata, module,
451 			    "Error flushing data before directory write");
452 			return (0);
453                     }
454 		}
455 		if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
456 		{
457 			_TIFFfree(tif->tif_rawdata);
458 			tif->tif_rawdata = NULL;
459 			tif->tif_rawcc = 0;
460 			tif->tif_rawdatasize = 0;
461                         tif->tif_rawdataoff = 0;
462                         tif->tif_rawdataloaded = 0;
463 		}
464 		tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
465 	}
466 	dir=NULL;
467 	dirmem=NULL;
468 	dirsize=0;
469 	while (1)
470 	{
471 		ndir=0;
472 		if (isimage)
473 		{
474 			if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
475 			{
476 				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
477 					goto bad;
478 				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
479 					goto bad;
480 			}
481 			if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
482 			{
483 				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
484 					goto bad;
485 				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
486 					goto bad;
487 			}
488 			if (TIFFFieldSet(tif,FIELD_RESOLUTION))
489 			{
490 				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
491 					goto bad;
492 				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
493 					goto bad;
494 			}
495 			if (TIFFFieldSet(tif,FIELD_POSITION))
496 			{
497 				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
498 					goto bad;
499 				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
500 					goto bad;
501 			}
502 			if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
503 			{
504 				if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
505 					goto bad;
506 			}
507 			if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
508 			{
509 				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
510 					goto bad;
511 			}
512 			if (TIFFFieldSet(tif,FIELD_COMPRESSION))
513 			{
514 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
515 					goto bad;
516 			}
517 			if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
518 			{
519 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
520 					goto bad;
521 			}
522 			if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
523 			{
524 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
525 					goto bad;
526 			}
527 			if (TIFFFieldSet(tif,FIELD_FILLORDER))
528 			{
529 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
530 					goto bad;
531 			}
532 			if (TIFFFieldSet(tif,FIELD_ORIENTATION))
533 			{
534 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
535 					goto bad;
536 			}
537 			if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
538 			{
539 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
540 					goto bad;
541 			}
542 			if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
543 			{
544 				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
545 					goto bad;
546 			}
547 			if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
548 			{
549 				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
550 					goto bad;
551 			}
552 			if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
553 			{
554 				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
555 					goto bad;
556 			}
557 			if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
558 			{
559 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
560 					goto bad;
561 			}
562 			if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
563 			{
564 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
565 					goto bad;
566 			}
567 			if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
568 			{
569 				if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
570 					goto bad;
571 			}
572 			if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
573 			{
574 				if (!isTiled(tif))
575 				{
576 					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount_p))
577 						goto bad;
578 				}
579 				else
580 				{
581 					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount_p))
582 						goto bad;
583 				}
584 			}
585 			if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
586 			{
587 				if (!isTiled(tif))
588 				{
589                     /* td_stripoffset_p might be NULL in an odd OJPEG case. See
590                      *  tif_dirread.c around line 3634.
591                      * XXX: OJPEG hack.
592                      * If a) compression is OJPEG, b) it's not a tiled TIFF,
593                      * and c) the number of strips is 1,
594                      * then we tolerate the absence of stripoffsets tag,
595                      * because, presumably, all required data is in the
596                      * JpegInterchangeFormat stream.
597                      * We can get here when using tiffset on such a file.
598                      * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
599                     */
600                     if (tif->tif_dir.td_stripoffset_p != NULL &&
601                         !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset_p))
602                         goto bad;
603 				}
604 				else
605 				{
606 					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset_p))
607 						goto bad;
608 				}
609 			}
610 			if (TIFFFieldSet(tif,FIELD_COLORMAP))
611 			{
612 				if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
613 					goto bad;
614 			}
615 			if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
616 			{
617 				if (tif->tif_dir.td_extrasamples)
618 				{
619 					uint16 na;
620 					uint16* nb;
621 					TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
622 					if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
623 						goto bad;
624 				}
625 			}
626 			if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
627 			{
628 				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
629 					goto bad;
630 			}
631 			if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
632 			{
633 				if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
634 					goto bad;
635 			}
636 			if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
637 			{
638 				if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
639 					goto bad;
640 			}
641 			if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
642 			{
643 				if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
644 					goto bad;
645 			}
646 			if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
647 			{
648 				if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
649 					goto bad;
650 			}
651 			if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
652 			{
653 				if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
654 					goto bad;
655 			}
656 			if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
657 			{
658 				if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
659 					goto bad;
660 			}
661 			if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
662 			{
663 				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
664 					goto bad;
665 			}
666 			if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
667 			{
668 				if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
669 					goto bad;
670 			}
671 			if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
672 			{
673 				if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
674 					goto bad;
675 			}
676 			if (TIFFFieldSet(tif,FIELD_INKNAMES))
677 			{
678 				if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
679 					goto bad;
680 			}
681 			if (TIFFFieldSet(tif,FIELD_SUBIFD))
682 			{
683 				if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
684 					goto bad;
685 			}
686 			{
687 				uint32 n;
688 				for (n=0; n<tif->tif_nfields; n++) {
689 					const TIFFField* o;
690 					o = tif->tif_fields[n];
691 					if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
692 					{
693 						switch (o->get_field_type)
694 						{
695 							case TIFF_SETGET_ASCII:
696 								{
697 									uint32 pa;
698 									char* pb;
699 									assert(o->field_type==TIFF_ASCII);
700 									assert(o->field_readcount==TIFF_VARIABLE);
701 									assert(o->field_passcount==0);
702 									TIFFGetField(tif,o->field_tag,&pb);
703 									pa=(uint32)(strlen(pb));
704 									if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
705 										goto bad;
706 								}
707 								break;
708 							case TIFF_SETGET_UINT16:
709 								{
710 									uint16 p;
711 									assert(o->field_type==TIFF_SHORT);
712 									assert(o->field_readcount==1);
713 									assert(o->field_passcount==0);
714 									TIFFGetField(tif,o->field_tag,&p);
715 									if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))
716 										goto bad;
717 								}
718 								break;
719 							case TIFF_SETGET_UINT32:
720 								{
721 									uint32 p;
722 									assert(o->field_type==TIFF_LONG);
723 									assert(o->field_readcount==1);
724 									assert(o->field_passcount==0);
725 									TIFFGetField(tif,o->field_tag,&p);
726 									if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))
727 										goto bad;
728 								}
729 								break;
730 							case TIFF_SETGET_C32_UINT8:
731 								{
732 									uint32 pa;
733 									void* pb;
734 									assert(o->field_type==TIFF_UNDEFINED);
735 									assert(o->field_readcount==TIFF_VARIABLE2);
736 									assert(o->field_passcount==1);
737 									TIFFGetField(tif,o->field_tag,&pa,&pb);
738 									if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
739 										goto bad;
740 								}
741 								break;
742 							default:
743 								TIFFErrorExt(tif->tif_clientdata,module,
744 								            "Cannot write tag %d (%s)",
745 								            TIFFFieldTag(o),
746                                                                             o->field_name ? o->field_name : "unknown");
747 								goto bad;
748 						}
749 					}
750 				}
751 			}
752 		}
753 		for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
754 		{
755                         uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;
756                         uint32 count = tif->tif_dir.td_customValues[m].count;
757 			switch (tif->tif_dir.td_customValues[m].info->field_type)
758 			{
759 				case TIFF_ASCII:
760 					if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
761 						goto bad;
762 					break;
763 				case TIFF_UNDEFINED:
764 					if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
765 						goto bad;
766 					break;
767 				case TIFF_BYTE:
768 					if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
769 						goto bad;
770 					break;
771 				case TIFF_SBYTE:
772 					if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
773 						goto bad;
774 					break;
775 				case TIFF_SHORT:
776 					if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
777 						goto bad;
778 					break;
779 				case TIFF_SSHORT:
780 					if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
781 						goto bad;
782 					break;
783 				case TIFF_LONG:
784 					if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
785 						goto bad;
786 					break;
787 				case TIFF_SLONG:
788 					if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
789 						goto bad;
790 					break;
791 				case TIFF_LONG8:
792 					if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
793 						goto bad;
794 					break;
795 				case TIFF_SLONG8:
796 					if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
797 						goto bad;
798 					break;
799 				case TIFF_RATIONAL:
800 					if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
801 						goto bad;
802 					break;
803 				case TIFF_SRATIONAL:
804 					if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
805 						goto bad;
806 					break;
807 				case TIFF_FLOAT:
808 					if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
809 						goto bad;
810 					break;
811 				case TIFF_DOUBLE:
812 					if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
813 						goto bad;
814 					break;
815 				case TIFF_IFD:
816 					if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
817 						goto bad;
818 					break;
819 				case TIFF_IFD8:
820 					if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
821 						goto bad;
822 					break;
823 				default:
824 					assert(0);   /* we should never get here */
825 					break;
826 			}
827 		}
828 		if (dir!=NULL)
829 			break;
830 		dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
831 		if (dir==NULL)
832 		{
833 			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
834 			goto bad;
835 		}
836 		if (isimage)
837 		{
838 			if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
839 				goto bad;
840 		}
841 		else
842 			tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));
843 		if (pdiroff!=NULL)
844 			*pdiroff=tif->tif_diroff;
845 		if (!(tif->tif_flags&TIFF_BIGTIFF))
846 			dirsize=2+ndir*12+4;
847 		else
848 			dirsize=8+ndir*20+8;
849 		tif->tif_dataoff=tif->tif_diroff+dirsize;
850 		if (!(tif->tif_flags&TIFF_BIGTIFF))
851 			tif->tif_dataoff=(uint32)tif->tif_dataoff;
852 		if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))
853 		{
854 			TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
855 			goto bad;
856 		}
857 		if (tif->tif_dataoff&1)
858 			tif->tif_dataoff++;
859 		if (isimage)
860 			tif->tif_curdir++;
861 	}
862 	if (isimage)
863 	{
864 		if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
865 		{
866 			uint32 na;
867 			TIFFDirEntry* nb;
868 			for (na=0, nb=dir; ; na++, nb++)
869 			{
870 				if( na == ndir )
871                                 {
872                                     TIFFErrorExt(tif->tif_clientdata,module,
873                                                  "Cannot find SubIFD tag");
874                                     goto bad;
875                                 }
876 				if (nb->tdir_tag==TIFFTAG_SUBIFD)
877 					break;
878 			}
879 			if (!(tif->tif_flags&TIFF_BIGTIFF))
880 				tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
881 			else
882 				tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
883 		}
884 	}
885 	dirmem=_TIFFmalloc(dirsize);
886 	if (dirmem==NULL)
887 	{
888 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
889 		goto bad;
890 	}
891 	if (!(tif->tif_flags&TIFF_BIGTIFF))
892 	{
893 		uint8* n;
894 		uint32 nTmp;
895 		TIFFDirEntry* o;
896 		n=dirmem;
897 		*(uint16*)n=(uint16)ndir;
898 		if (tif->tif_flags&TIFF_SWAB)
899 			TIFFSwabShort((uint16*)n);
900 		n+=2;
901 		o=dir;
902 		for (m=0; m<ndir; m++)
903 		{
904 			*(uint16*)n=o->tdir_tag;
905 			if (tif->tif_flags&TIFF_SWAB)
906 				TIFFSwabShort((uint16*)n);
907 			n+=2;
908 			*(uint16*)n=o->tdir_type;
909 			if (tif->tif_flags&TIFF_SWAB)
910 				TIFFSwabShort((uint16*)n);
911 			n+=2;
912 			nTmp = (uint32)o->tdir_count;
913 			_TIFFmemcpy(n,&nTmp,4);
914 			if (tif->tif_flags&TIFF_SWAB)
915 				TIFFSwabLong((uint32*)n);
916 			n+=4;
917 			/* This is correct. The data has been */
918 			/* swabbed previously in TIFFWriteDirectoryTagData */
919 			_TIFFmemcpy(n,&o->tdir_offset,4);
920 			n+=4;
921 			o++;
922 		}
923 		nTmp = (uint32)tif->tif_nextdiroff;
924 		if (tif->tif_flags&TIFF_SWAB)
925 			TIFFSwabLong(&nTmp);
926 		_TIFFmemcpy(n,&nTmp,4);
927 	}
928 	else
929 	{
930 		uint8* n;
931 		TIFFDirEntry* o;
932 		n=dirmem;
933 		*(uint64*)n=ndir;
934 		if (tif->tif_flags&TIFF_SWAB)
935 			TIFFSwabLong8((uint64*)n);
936 		n+=8;
937 		o=dir;
938 		for (m=0; m<ndir; m++)
939 		{
940 			*(uint16*)n=o->tdir_tag;
941 			if (tif->tif_flags&TIFF_SWAB)
942 				TIFFSwabShort((uint16*)n);
943 			n+=2;
944 			*(uint16*)n=o->tdir_type;
945 			if (tif->tif_flags&TIFF_SWAB)
946 				TIFFSwabShort((uint16*)n);
947 			n+=2;
948 			_TIFFmemcpy(n,&o->tdir_count,8);
949 			if (tif->tif_flags&TIFF_SWAB)
950 				TIFFSwabLong8((uint64*)n);
951 			n+=8;
952 			_TIFFmemcpy(n,&o->tdir_offset,8);
953 			n+=8;
954 			o++;
955 		}
956 		_TIFFmemcpy(n,&tif->tif_nextdiroff,8);
957 		if (tif->tif_flags&TIFF_SWAB)
958 			TIFFSwabLong8((uint64*)n);
959 	}
960 	_TIFFfree(dir);
961 	dir=NULL;
962 	if (!SeekOK(tif,tif->tif_diroff))
963 	{
964 		TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
965 		goto bad;
966 	}
967 	if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
968 	{
969 		TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
970 		goto bad;
971 	}
972 	_TIFFfree(dirmem);
973 	if (imagedone)
974 	{
975 		TIFFFreeDirectory(tif);
976 		tif->tif_flags &= ~TIFF_DIRTYDIRECT;
977 		tif->tif_flags &= ~TIFF_DIRTYSTRIP;
978 		(*tif->tif_cleanup)(tif);
979 		/*
980 		* Reset directory-related state for subsequent
981 		* directories.
982 		*/
983 		TIFFCreateDirectory(tif);
984 	}
985 	return(1);
986 bad:
987 	if (dir!=NULL)
988 		_TIFFfree(dir);
989 	if (dirmem!=NULL)
990 		_TIFFfree(dirmem);
991 	return(0);
992 }
993 
994 static int8 TIFFClampDoubleToInt8( double val )
995 {
996     if( val > 127 )
997         return 127;
998     if( val < -128 || val != val )
999         return -128;
1000     return (int8)val;
1001 }
1002 
1003 static int16 TIFFClampDoubleToInt16( double val )
1004 {
1005     if( val > 32767 )
1006         return 32767;
1007     if( val < -32768 || val != val )
1008         return -32768;
1009     return (int16)val;
1010 }
1011 
1012 static int32 TIFFClampDoubleToInt32( double val )
1013 {
1014     if( val > 0x7FFFFFFF )
1015         return 0x7FFFFFFF;
1016     if( val < -0x7FFFFFFF-1 || val != val )
1017         return -0x7FFFFFFF-1;
1018     return (int32)val;
1019 }
1020 
1021 static uint8 TIFFClampDoubleToUInt8( double val )
1022 {
1023     if( val < 0 )
1024         return 0;
1025     if( val > 255 || val != val )
1026         return 255;
1027     return (uint8)val;
1028 }
1029 
1030 static uint16 TIFFClampDoubleToUInt16( double val )
1031 {
1032     if( val < 0 )
1033         return 0;
1034     if( val > 65535 || val != val )
1035         return 65535;
1036     return (uint16)val;
1037 }
1038 
1039 static uint32 TIFFClampDoubleToUInt32( double val )
1040 {
1041     if( val < 0 )
1042         return 0;
1043     if( val > 0xFFFFFFFFU || val != val )
1044         return 0xFFFFFFFFU;
1045     return (uint32)val;
1046 }
1047 
1048 static int
1049 TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
1050 {
1051 	static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
1052 	void* conv;
1053 	uint32 i;
1054 	int ok;
1055 	conv = _TIFFmalloc(count*sizeof(double));
1056 	if (conv == NULL)
1057 	{
1058 		TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
1059 		return (0);
1060 	}
1061 
1062 	switch (tif->tif_dir.td_sampleformat)
1063 	{
1064 		case SAMPLEFORMAT_IEEEFP:
1065 			if (tif->tif_dir.td_bitspersample<=32)
1066 			{
1067 				for (i = 0; i < count; ++i)
1068 					((float*)conv)[i] = _TIFFClampDoubleToFloat(value[i]);
1069 				ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
1070 			}
1071 			else
1072 			{
1073 				ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
1074 			}
1075 			break;
1076 		case SAMPLEFORMAT_INT:
1077 			if (tif->tif_dir.td_bitspersample<=8)
1078 			{
1079 				for (i = 0; i < count; ++i)
1080 					((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
1081 				ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
1082 			}
1083 			else if (tif->tif_dir.td_bitspersample<=16)
1084 			{
1085 				for (i = 0; i < count; ++i)
1086 					((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
1087 				ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
1088 			}
1089 			else
1090 			{
1091 				for (i = 0; i < count; ++i)
1092 					((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
1093 				ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
1094 			}
1095 			break;
1096 		case SAMPLEFORMAT_UINT:
1097 			if (tif->tif_dir.td_bitspersample<=8)
1098 			{
1099 				for (i = 0; i < count; ++i)
1100 					((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
1101 				ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
1102 			}
1103 			else if (tif->tif_dir.td_bitspersample<=16)
1104 			{
1105 				for (i = 0; i < count; ++i)
1106 					((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
1107 				ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
1108 			}
1109 			else
1110 			{
1111 				for (i = 0; i < count; ++i)
1112 					((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
1113 				ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
1114 			}
1115 			break;
1116 		default:
1117 			ok = 0;
1118 	}
1119 
1120 	_TIFFfree(conv);
1121 	return (ok);
1122 }
1123 
1124 #if 0
1125 static int
1126 TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1127 {
1128 	switch (tif->tif_dir.td_sampleformat)
1129 	{
1130 		case SAMPLEFORMAT_IEEEFP:
1131 			if (tif->tif_dir.td_bitspersample<=32)
1132 				return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
1133 			else
1134 				return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
1135 		case SAMPLEFORMAT_INT:
1136 			if (tif->tif_dir.td_bitspersample<=8)
1137 				return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));
1138 			else if (tif->tif_dir.td_bitspersample<=16)
1139 				return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));
1140 			else
1141 				return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));
1142 		case SAMPLEFORMAT_UINT:
1143 			if (tif->tif_dir.td_bitspersample<=8)
1144 				return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));
1145 			else if (tif->tif_dir.td_bitspersample<=16)
1146 				return(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16)value));
1147 			else
1148 				return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));
1149 		default:
1150 			return(1);
1151 	}
1152 }
1153 #endif
1154 
1155 static int
1156 TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
1157 {
1158 	if (dir==NULL)
1159 	{
1160 		(*ndir)++;
1161 		return(1);
1162 	}
1163 	return(TIFFWriteDirectoryTagCheckedAscii(tif,ndir,dir,tag,count,value));
1164 }
1165 
1166 static int
1167 TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
1168 {
1169 	if (dir==NULL)
1170 	{
1171 		(*ndir)++;
1172 		return(1);
1173 	}
1174 	return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif,ndir,dir,tag,count,value));
1175 }
1176 
1177 #ifdef notdef
1178 static int
1179 TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
1180 {
1181 	if (dir==NULL)
1182 	{
1183 		(*ndir)++;
1184 		return(1);
1185 	}
1186 	return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
1187 }
1188 #endif
1189 
1190 static int
1191 TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
1192 {
1193 	if (dir==NULL)
1194 	{
1195 		(*ndir)++;
1196 		return(1);
1197 	}
1198 	return(TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,count,value));
1199 }
1200 
1201 #if 0
1202 static int
1203 TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
1204 {
1205 	static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
1206 	uint8* m;
1207 	uint8* na;
1208 	uint16 nb;
1209 	int o;
1210 	if (dir==NULL)
1211 	{
1212 		(*ndir)++;
1213 		return(1);
1214 	}
1215 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint8));
1216 	if (m==NULL)
1217 	{
1218 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1219 		return(0);
1220 	}
1221 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1222 		*na=value;
1223 	o=TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1224 	_TIFFfree(m);
1225 	return(o);
1226 }
1227 #endif
1228 
1229 #ifdef notdef
1230 static int
1231 TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
1232 {
1233 	if (dir==NULL)
1234 	{
1235 		(*ndir)++;
1236 		return(1);
1237 	}
1238 	return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
1239 }
1240 #endif
1241 
1242 static int
1243 TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
1244 {
1245 	if (dir==NULL)
1246 	{
1247 		(*ndir)++;
1248 		return(1);
1249 	}
1250 	return(TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,count,value));
1251 }
1252 
1253 #if 0
1254 static int
1255 TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
1256 {
1257 	static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
1258 	int8* m;
1259 	int8* na;
1260 	uint16 nb;
1261 	int o;
1262 	if (dir==NULL)
1263 	{
1264 		(*ndir)++;
1265 		return(1);
1266 	}
1267 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int8));
1268 	if (m==NULL)
1269 	{
1270 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1271 		return(0);
1272 	}
1273 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1274 		*na=value;
1275 	o=TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1276 	_TIFFfree(m);
1277 	return(o);
1278 }
1279 #endif
1280 
1281 static int
1282 TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
1283 {
1284 	if (dir==NULL)
1285 	{
1286 		(*ndir)++;
1287 		return(1);
1288 	}
1289 	return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,value));
1290 }
1291 
1292 static int
1293 TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
1294 {
1295 	if (dir==NULL)
1296 	{
1297 		(*ndir)++;
1298 		return(1);
1299 	}
1300 	return(TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,value));
1301 }
1302 
1303 static int
1304 TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
1305 {
1306 	static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
1307 	uint16* m;
1308 	uint16* na;
1309 	uint16 nb;
1310 	int o;
1311 	if (dir==NULL)
1312 	{
1313 		(*ndir)++;
1314 		return(1);
1315 	}
1316 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
1317 	if (m==NULL)
1318 	{
1319 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1320 		return(0);
1321 	}
1322 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1323 		*na=value;
1324 	o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1325 	_TIFFfree(m);
1326 	return(o);
1327 }
1328 
1329 #ifdef notdef
1330 static int
1331 TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
1332 {
1333 	if (dir==NULL)
1334 	{
1335 		(*ndir)++;
1336 		return(1);
1337 	}
1338 	return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
1339 }
1340 #endif
1341 
1342 static int
1343 TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
1344 {
1345 	if (dir==NULL)
1346 	{
1347 		(*ndir)++;
1348 		return(1);
1349 	}
1350 	return(TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,count,value));
1351 }
1352 
1353 #if 0
1354 static int
1355 TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
1356 {
1357 	static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
1358 	int16* m;
1359 	int16* na;
1360 	uint16 nb;
1361 	int o;
1362 	if (dir==NULL)
1363 	{
1364 		(*ndir)++;
1365 		return(1);
1366 	}
1367 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int16));
1368 	if (m==NULL)
1369 	{
1370 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1371 		return(0);
1372 	}
1373 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1374 		*na=value;
1375 	o=TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1376 	_TIFFfree(m);
1377 	return(o);
1378 }
1379 #endif
1380 
1381 static int
1382 TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1383 {
1384 	if (dir==NULL)
1385 	{
1386 		(*ndir)++;
1387 		return(1);
1388 	}
1389 	return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
1390 }
1391 
1392 static int
1393 TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
1394 {
1395 	if (dir==NULL)
1396 	{
1397 		(*ndir)++;
1398 		return(1);
1399 	}
1400 	return(TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,value));
1401 }
1402 
1403 #if 0
1404 static int
1405 TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1406 {
1407 	static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
1408 	uint32* m;
1409 	uint32* na;
1410 	uint16 nb;
1411 	int o;
1412 	if (dir==NULL)
1413 	{
1414 		(*ndir)++;
1415 		return(1);
1416 	}
1417 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint32));
1418 	if (m==NULL)
1419 	{
1420 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1421 		return(0);
1422 	}
1423 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1424 		*na=value;
1425 	o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1426 	_TIFFfree(m);
1427 	return(o);
1428 }
1429 #endif
1430 
1431 #ifdef notdef
1432 static int
1433 TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
1434 {
1435 	if (dir==NULL)
1436 	{
1437 		(*ndir)++;
1438 		return(1);
1439 	}
1440 	return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
1441 }
1442 #endif
1443 
1444 static int
1445 TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
1446 {
1447 	if (dir==NULL)
1448 	{
1449 		(*ndir)++;
1450 		return(1);
1451 	}
1452 	return(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));
1453 }
1454 
1455 #if 0
1456 static int
1457 TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
1458 {
1459 	static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
1460 	int32* m;
1461 	int32* na;
1462 	uint16 nb;
1463 	int o;
1464 	if (dir==NULL)
1465 	{
1466 		(*ndir)++;
1467 		return(1);
1468 	}
1469 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32));
1470 	if (m==NULL)
1471 	{
1472 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1473 		return(0);
1474 	}
1475 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1476 		*na=value;
1477 	o=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1478 	_TIFFfree(m);
1479 	return(o);
1480 }
1481 #endif
1482 
1483 #ifdef notdef
1484 static int
1485 TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
1486 {
1487 	if (dir==NULL)
1488 	{
1489 		(*ndir)++;
1490 		return(1);
1491 	}
1492 	return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
1493 }
1494 #endif
1495 
1496 static int
1497 TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1498 {
1499 	if (dir==NULL)
1500 	{
1501 		(*ndir)++;
1502 		return(1);
1503 	}
1504 	return(TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value));
1505 }
1506 
1507 #ifdef notdef
1508 static int
1509 TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
1510 {
1511 	if (dir==NULL)
1512 	{
1513 		(*ndir)++;
1514 		return(1);
1515 	}
1516 	return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
1517 }
1518 #endif
1519 
1520 static int
1521 TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
1522 {
1523 	if (dir==NULL)
1524 	{
1525 		(*ndir)++;
1526 		return(1);
1527 	}
1528 	return(TIFFWriteDirectoryTagCheckedSlong8Array(tif,ndir,dir,tag,count,value));
1529 }
1530 
1531 static int
1532 TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1533 {
1534 	if (dir==NULL)
1535 	{
1536 		(*ndir)++;
1537 		return(1);
1538 	}
1539 	return(TIFFWriteDirectoryTagCheckedRational(tif,ndir,dir,tag,value));
1540 }
1541 
1542 static int
1543 TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
1544 {
1545 	if (dir==NULL)
1546 	{
1547 		(*ndir)++;
1548 		return(1);
1549 	}
1550 	return(TIFFWriteDirectoryTagCheckedRationalArray(tif,ndir,dir,tag,count,value));
1551 }
1552 
1553 static int
1554 TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
1555 {
1556 	if (dir==NULL)
1557 	{
1558 		(*ndir)++;
1559 		return(1);
1560 	}
1561 	return(TIFFWriteDirectoryTagCheckedSrationalArray(tif,ndir,dir,tag,count,value));
1562 }
1563 
1564 #ifdef notdef
1565 static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
1566 {
1567 	if (dir==NULL)
1568 	{
1569 		(*ndir)++;
1570 		return(1);
1571 	}
1572 	return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
1573 }
1574 #endif
1575 
1576 static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
1577 {
1578 	if (dir==NULL)
1579 	{
1580 		(*ndir)++;
1581 		return(1);
1582 	}
1583 	return(TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,count,value));
1584 }
1585 
1586 #if 0
1587 static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
1588 {
1589 	static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
1590 	float* m;
1591 	float* na;
1592 	uint16 nb;
1593 	int o;
1594 	if (dir==NULL)
1595 	{
1596 		(*ndir)++;
1597 		return(1);
1598 	}
1599 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
1600 	if (m==NULL)
1601 	{
1602 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1603 		return(0);
1604 	}
1605 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1606 		*na=value;
1607 	o=TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1608 	_TIFFfree(m);
1609 	return(o);
1610 }
1611 #endif
1612 
1613 #ifdef notdef
1614 static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1615 {
1616 	if (dir==NULL)
1617 	{
1618 		(*ndir)++;
1619 		return(1);
1620 	}
1621 	return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
1622 }
1623 #endif
1624 
1625 static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
1626 {
1627 	if (dir==NULL)
1628 	{
1629 		(*ndir)++;
1630 		return(1);
1631 	}
1632 	return(TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,count,value));
1633 }
1634 
1635 #if 0
1636 static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1637 {
1638 	static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
1639 	double* m;
1640 	double* na;
1641 	uint16 nb;
1642 	int o;
1643 	if (dir==NULL)
1644 	{
1645 		(*ndir)++;
1646 		return(1);
1647 	}
1648 	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
1649 	if (m==NULL)
1650 	{
1651 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1652 		return(0);
1653 	}
1654 	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1655 		*na=value;
1656 	o=TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1657 	_TIFFfree(m);
1658 	return(o);
1659 }
1660 #endif
1661 
1662 static int
1663 TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
1664 {
1665 	if (dir==NULL)
1666 	{
1667 		(*ndir)++;
1668 		return(1);
1669 	}
1670 	return(TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,value));
1671 }
1672 
1673 #ifdef notdef
1674 static int
1675 TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1676 {
1677 	if (dir==NULL)
1678 	{
1679 		(*ndir)++;
1680 		return(1);
1681 	}
1682 	return(TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,tag,count,value));
1683 }
1684 #endif
1685 
1686 static int
1687 TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1688 {
1689 	if (dir==NULL)
1690 	{
1691 		(*ndir)++;
1692 		return(1);
1693 	}
1694 	if (value<=0xFFFF)
1695 		return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,(uint16)value));
1696 	else
1697 		return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
1698 }
1699 
1700 static int _WriteAsType(TIFF* tif, uint64 strile_size, uint64 uncompressed_threshold)
1701 {
1702     const uint16 compression = tif->tif_dir.td_compression;
1703     if ( compression == COMPRESSION_NONE )
1704     {
1705         return strile_size > uncompressed_threshold;
1706     }
1707     else if ( compression == COMPRESSION_JPEG ||
1708               compression == COMPRESSION_LZW ||
1709               compression == COMPRESSION_ADOBE_DEFLATE ||
1710               compression == COMPRESSION_LZMA ||
1711               compression == COMPRESSION_LERC ||
1712               compression == COMPRESSION_ZSTD ||
1713               compression == COMPRESSION_WEBP )
1714     {
1715         /* For a few select compression types, we assume that in the worst */
1716         /* case the compressed size will be 10 times the uncompressed size */
1717         /* This is overly pessismistic ! */
1718         return strile_size >= uncompressed_threshold / 10;
1719     }
1720     return 1;
1721 }
1722 
1723 static int WriteAsLong8(TIFF* tif, uint64 strile_size)
1724 {
1725     return _WriteAsType(tif, strile_size, 0xFFFFFFFFU);
1726 }
1727 
1728 static int WriteAsLong4(TIFF* tif, uint64 strile_size)
1729 {
1730     return _WriteAsType(tif, strile_size, 0xFFFFU);
1731 }
1732 
1733 /************************************************************************/
1734 /*                TIFFWriteDirectoryTagLongLong8Array()                 */
1735 /*                                                                      */
1736 /*      Write out LONG8 array and write a SHORT/LONG/LONG8 depending    */
1737 /*      on strile size and Classic/BigTIFF mode.                        */
1738 /************************************************************************/
1739 
1740 static int
1741 TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1742 {
1743     static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
1744     int o;
1745     int write_aslong4;
1746 
1747     /* is this just a counting pass? */
1748     if (dir==NULL)
1749     {
1750         (*ndir)++;
1751         return(1);
1752     }
1753 
1754     if( tif->tif_dir.td_deferstrilearraywriting )
1755     {
1756         return TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_NOTYPE, 0, 0, NULL);
1757     }
1758 
1759     if( tif->tif_flags&TIFF_BIGTIFF )
1760     {
1761         int write_aslong8 = 1;
1762         /* In the case of ByteCounts array, we may be able to write them on */
1763         /* LONG if the strip/tilesize is not too big. */
1764         /* Also do that for count > 1 in the case someone would want to create */
1765         /* a single-strip file with a growing height, in which case using */
1766         /* LONG8 will be safer. */
1767         if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
1768         {
1769             write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
1770         }
1771         else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
1772         {
1773             write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
1774         }
1775         if( write_aslong8 )
1776         {
1777             return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,
1778                                                         tag,count,value);
1779         }
1780     }
1781 
1782     write_aslong4 = 1;
1783     if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
1784     {
1785         write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
1786     }
1787     else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
1788     {
1789         write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
1790     }
1791     if( write_aslong4 )
1792     {
1793         /*
1794         ** For classic tiff we want to verify everything is in range for LONG
1795         ** and convert to long format.
1796         */
1797 
1798         uint32* p = _TIFFmalloc(count*sizeof(uint32));
1799         uint32* q;
1800         uint64* ma;
1801         uint32 mb;
1802 
1803         if (p==NULL)
1804         {
1805             TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1806             return(0);
1807         }
1808 
1809         for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1810         {
1811             if (*ma>0xFFFFFFFF)
1812             {
1813                 TIFFErrorExt(tif->tif_clientdata,module,
1814                             "Attempt to write value larger than 0xFFFFFFFF in LONG array.");
1815                 _TIFFfree(p);
1816                 return(0);
1817             }
1818             *q= (uint32)(*ma);
1819         }
1820 
1821         o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
1822         _TIFFfree(p);
1823     }
1824     else
1825     {
1826         uint16* p = _TIFFmalloc(count*sizeof(uint16));
1827         uint16* q;
1828         uint64* ma;
1829         uint32 mb;
1830 
1831         if (p==NULL)
1832         {
1833             TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1834             return(0);
1835         }
1836 
1837         for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1838         {
1839             if (*ma>0xFFFF)
1840             {
1841                 /* Should not happen normally given the check we did before */
1842                 TIFFErrorExt(tif->tif_clientdata,module,
1843                             "Attempt to write value larger than 0xFFFF in SHORT array.");
1844                 _TIFFfree(p);
1845                 return(0);
1846             }
1847             *q= (uint16)(*ma);
1848         }
1849 
1850         o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
1851         _TIFFfree(p);
1852     }
1853 
1854     return(o);
1855 }
1856 
1857 /************************************************************************/
1858 /*                 TIFFWriteDirectoryTagIfdIfd8Array()                  */
1859 /*                                                                      */
1860 /*      Write either IFD8 or IFD array depending on file type.          */
1861 /************************************************************************/
1862 
1863 static int
1864 TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1865 {
1866     static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1867     uint64* ma;
1868     uint32 mb;
1869     uint32* p;
1870     uint32* q;
1871     int o;
1872 
1873     /* is this just a counting pass? */
1874     if (dir==NULL)
1875     {
1876         (*ndir)++;
1877         return(1);
1878     }
1879 
1880     /* We always write IFD8 for BigTIFF, no checking needed. */
1881     if( tif->tif_flags&TIFF_BIGTIFF )
1882         return TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,
1883                                                      tag,count,value);
1884 
1885     /*
1886     ** For classic tiff we want to verify everything is in range for IFD
1887     ** and convert to long format.
1888     */
1889 
1890     p = _TIFFmalloc(count*sizeof(uint32));
1891     if (p==NULL)
1892     {
1893         TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1894         return(0);
1895     }
1896 
1897     for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1898     {
1899         if (*ma>0xFFFFFFFF)
1900         {
1901             TIFFErrorExt(tif->tif_clientdata,module,
1902                          "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1903             _TIFFfree(p);
1904             return(0);
1905         }
1906         *q= (uint32)(*ma);
1907     }
1908 
1909     o=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,p);
1910     _TIFFfree(p);
1911 
1912     return(o);
1913 }
1914 
1915 #ifdef notdef
1916 static int
1917 TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1918 {
1919 	static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1920 	uint64* ma;
1921 	uint32 mb;
1922 	uint8 n;
1923 	int o;
1924 	if (dir==NULL)
1925 	{
1926 		(*ndir)++;
1927 		return(1);
1928 	}
1929 	n=0;
1930 	for (ma=value, mb=0; mb<count; ma++, mb++)
1931 	{
1932 		if ((n==0)&&(*ma>0xFFFF))
1933 			n=1;
1934 		if ((n==1)&&(*ma>0xFFFFFFFF))
1935 		{
1936 			n=2;
1937 			break;
1938 		}
1939 	}
1940 	if (n==0)
1941 	{
1942 		uint16* p;
1943 		uint16* q;
1944 		p=_TIFFmalloc(count*sizeof(uint16));
1945 		if (p==NULL)
1946 		{
1947 			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1948 			return(0);
1949 		}
1950 		for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
1951 			*q=(uint16)(*ma);
1952 		o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
1953 		_TIFFfree(p);
1954 	}
1955 	else if (n==1)
1956 	{
1957 		uint32* p;
1958 		uint32* q;
1959 		p=_TIFFmalloc(count*sizeof(uint32));
1960 		if (p==NULL)
1961 		{
1962 			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1963 			return(0);
1964 		}
1965 		for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
1966 			*q=(uint32)(*ma);
1967 		o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
1968 		_TIFFfree(p);
1969 	}
1970 	else
1971 	{
1972 		assert(n==2);
1973 		o=TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value);
1974 	}
1975 	return(o);
1976 }
1977 #endif
1978 static int
1979 TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
1980 {
1981 	static const char module[] = "TIFFWriteDirectoryTagColormap";
1982 	uint32 m;
1983 	uint16* n;
1984 	int o;
1985 	if (dir==NULL)
1986 	{
1987 		(*ndir)++;
1988 		return(1);
1989 	}
1990 	m=(1<<tif->tif_dir.td_bitspersample);
1991 	n=_TIFFmalloc(3*m*sizeof(uint16));
1992 	if (n==NULL)
1993 	{
1994 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1995 		return(0);
1996 	}
1997 	_TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16));
1998 	_TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16));
1999 	_TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16));
2000 	o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_COLORMAP,3*m,n);
2001 	_TIFFfree(n);
2002 	return(o);
2003 }
2004 
2005 static int
2006 TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
2007 {
2008 	static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
2009 	uint32 m;
2010 	uint16 n;
2011 	uint16* o;
2012 	int p;
2013 	if (dir==NULL)
2014 	{
2015 		(*ndir)++;
2016 		return(1);
2017 	}
2018 	m=(1<<tif->tif_dir.td_bitspersample);
2019 	n=tif->tif_dir.td_samplesperpixel-tif->tif_dir.td_extrasamples;
2020 	/*
2021 	 * Check if the table can be written as a single column,
2022 	 * or if it must be written as 3 columns.  Note that we
2023 	 * write a 3-column tag if there are 2 samples/pixel and
2024 	 * a single column of data won't suffice--hmm.
2025 	 */
2026 	if (n>3)
2027 		n=3;
2028 	if (n==3)
2029 	{
2030 		if (tif->tif_dir.td_transferfunction[2] == NULL ||
2031 		    !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16)))
2032 			n=2;
2033 	}
2034 	if (n==2)
2035 	{
2036 		if (tif->tif_dir.td_transferfunction[1] == NULL ||
2037 		    !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16)))
2038 			n=1;
2039 	}
2040 	if (n==0)
2041 		n=1;
2042 	o=_TIFFmalloc(n*m*sizeof(uint16));
2043 	if (o==NULL)
2044 	{
2045 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2046 		return(0);
2047 	}
2048 	_TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16));
2049 	if (n>1)
2050 		_TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16));
2051 	if (n>2)
2052 		_TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16));
2053 	p=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_TRANSFERFUNCTION,n*m,o);
2054 	_TIFFfree(o);
2055 	return(p);
2056 }
2057 
2058 static int
2059 TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
2060 {
2061 	static const char module[] = "TIFFWriteDirectoryTagSubifd";
2062 	uint64 m;
2063 	int n;
2064 	if (tif->tif_dir.td_nsubifd==0)
2065 		return(1);
2066 	if (dir==NULL)
2067 	{
2068 		(*ndir)++;
2069 		return(1);
2070 	}
2071 	m=tif->tif_dataoff;
2072 	if (!(tif->tif_flags&TIFF_BIGTIFF))
2073 	{
2074 		uint32* o;
2075 		uint64* pa;
2076 		uint32* pb;
2077 		uint16 p;
2078 		o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));
2079 		if (o==NULL)
2080 		{
2081 			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2082 			return(0);
2083 		}
2084 		pa=tif->tif_dir.td_subifd;
2085 		pb=o;
2086 		for (p=0; p < tif->tif_dir.td_nsubifd; p++)
2087 		{
2088                         assert(pa != 0);
2089 
2090                         /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
2091                         if( *pa > 0xFFFFFFFFUL)
2092                         {
2093                             TIFFErrorExt(tif->tif_clientdata,module,"Illegal value for SubIFD tag");
2094                             _TIFFfree(o);
2095                             return(0);
2096                         }
2097 			*pb++=(uint32)(*pa++);
2098 		}
2099 		n=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);
2100 		_TIFFfree(o);
2101 	}
2102 	else
2103 		n=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);
2104 	if (!n)
2105 		return(0);
2106 	/*
2107 	 * Total hack: if this directory includes a SubIFD
2108 	 * tag then force the next <n> directories to be
2109 	 * written as ``sub directories'' of this one.  This
2110 	 * is used to write things like thumbnails and
2111 	 * image masks that one wants to keep out of the
2112 	 * normal directory linkage access mechanism.
2113 	 */
2114 	tif->tif_flags|=TIFF_INSUBIFD;
2115 	tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
2116 	if (tif->tif_dir.td_nsubifd==1)
2117 		tif->tif_subifdoff=0;
2118 	else
2119 		tif->tif_subifdoff=m;
2120 	return(1);
2121 }
2122 
2123 static int
2124 TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
2125 {
2126 	assert(sizeof(char)==1);
2127 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_ASCII,count,count,value));
2128 }
2129 
2130 static int
2131 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
2132 {
2133 	assert(sizeof(uint8)==1);
2134 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_UNDEFINED,count,count,value));
2135 }
2136 
2137 #ifdef notdef
2138 static int
2139 TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
2140 {
2141 	assert(sizeof(uint8)==1);
2142 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
2143 }
2144 #endif
2145 
2146 static int
2147 TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
2148 {
2149 	assert(sizeof(uint8)==1);
2150 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,count,count,value));
2151 }
2152 
2153 #ifdef notdef
2154 static int
2155 TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
2156 {
2157 	assert(sizeof(int8)==1);
2158 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
2159 }
2160 #endif
2161 
2162 static int
2163 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
2164 {
2165 	assert(sizeof(int8)==1);
2166 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,count,count,value));
2167 }
2168 
2169 static int
2170 TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
2171 {
2172 	uint16 m;
2173 	assert(sizeof(uint16)==2);
2174 	m=value;
2175 	if (tif->tif_flags&TIFF_SWAB)
2176 		TIFFSwabShort(&m);
2177 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
2178 }
2179 
2180 static int
2181 TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
2182 {
2183 	assert(count<0x80000000);
2184 	assert(sizeof(uint16)==2);
2185 	if (tif->tif_flags&TIFF_SWAB)
2186 		TIFFSwabArrayOfShort(value,count);
2187 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
2188 }
2189 
2190 #ifdef notdef
2191 static int
2192 TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
2193 {
2194 	int16 m;
2195 	assert(sizeof(int16)==2);
2196 	m=value;
2197 	if (tif->tif_flags&TIFF_SWAB)
2198 		TIFFSwabShort((uint16*)(&m));
2199 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
2200 }
2201 #endif
2202 
2203 static int
2204 TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
2205 {
2206 	assert(count<0x80000000);
2207 	assert(sizeof(int16)==2);
2208 	if (tif->tif_flags&TIFF_SWAB)
2209 		TIFFSwabArrayOfShort((uint16*)value,count);
2210 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,count,count*2,value));
2211 }
2212 
2213 static int
2214 TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
2215 {
2216 	uint32 m;
2217 	assert(sizeof(uint32)==4);
2218 	m=value;
2219 	if (tif->tif_flags&TIFF_SWAB)
2220 		TIFFSwabLong(&m);
2221 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
2222 }
2223 
2224 static int
2225 TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
2226 {
2227 	assert(count<0x40000000);
2228 	assert(sizeof(uint32)==4);
2229 	if (tif->tif_flags&TIFF_SWAB)
2230 		TIFFSwabArrayOfLong(value,count);
2231 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));
2232 }
2233 
2234 #ifdef notdef
2235 static int
2236 TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
2237 {
2238 	int32 m;
2239 	assert(sizeof(int32)==4);
2240 	m=value;
2241 	if (tif->tif_flags&TIFF_SWAB)
2242 		TIFFSwabLong((uint32*)(&m));
2243 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
2244 }
2245 #endif
2246 
2247 static int
2248 TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
2249 {
2250 	assert(count<0x40000000);
2251 	assert(sizeof(int32)==4);
2252 	if (tif->tif_flags&TIFF_SWAB)
2253 		TIFFSwabArrayOfLong((uint32*)value,count);
2254 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));
2255 }
2256 
2257 #ifdef notdef
2258 static int
2259 TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
2260 {
2261 	uint64 m;
2262 	assert(sizeof(uint64)==8);
2263 	if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2264 		TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
2265 		return(0);
2266 	}
2267 	m=value;
2268 	if (tif->tif_flags&TIFF_SWAB)
2269 		TIFFSwabLong8(&m);
2270 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
2271 }
2272 #endif
2273 
2274 static int
2275 TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
2276 {
2277 	assert(count<0x20000000);
2278 	assert(sizeof(uint64)==8);
2279 	if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2280 		TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
2281 		return(0);
2282 	}
2283 	if (tif->tif_flags&TIFF_SWAB)
2284 		TIFFSwabArrayOfLong8(value,count);
2285 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,count,count*8,value));
2286 }
2287 
2288 #ifdef notdef
2289 static int
2290 TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
2291 {
2292 	int64 m;
2293 	assert(sizeof(int64)==8);
2294 	if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2295 		TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
2296 		return(0);
2297 	}
2298 	m=value;
2299 	if (tif->tif_flags&TIFF_SWAB)
2300 		TIFFSwabLong8((uint64*)(&m));
2301 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
2302 }
2303 #endif
2304 
2305 static int
2306 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
2307 {
2308 	assert(count<0x20000000);
2309 	assert(sizeof(int64)==8);
2310 	if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2311 		TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
2312 		return(0);
2313 	}
2314 	if (tif->tif_flags&TIFF_SWAB)
2315 		TIFFSwabArrayOfLong8((uint64*)value,count);
2316 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,count,count*8,value));
2317 }
2318 
2319 static int
2320 TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
2321 {
2322         static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
2323 	uint32 m[2];
2324 	assert(sizeof(uint32)==4);
2325         if( value < 0 )
2326         {
2327             TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
2328             return 0;
2329         }
2330         else if( value != value )
2331         {
2332             TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
2333             return 0;
2334         }
2335 	else if (value==0.0)
2336 	{
2337 		m[0]=0;
2338 		m[1]=1;
2339 	}
2340 	else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
2341 	{
2342 		m[0]=(uint32)value;
2343 		m[1]=1;
2344 	}
2345 	else if (value<1.0)
2346 	{
2347 		m[0]=(uint32)(value*0xFFFFFFFF);
2348 		m[1]=0xFFFFFFFF;
2349 	}
2350 	else
2351 	{
2352 		m[0]=0xFFFFFFFF;
2353 		m[1]=(uint32)(0xFFFFFFFF/value);
2354 	}
2355 	if (tif->tif_flags&TIFF_SWAB)
2356 	{
2357 		TIFFSwabLong(&m[0]);
2358 		TIFFSwabLong(&m[1]);
2359 	}
2360 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
2361 }
2362 
2363 static int
2364 TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
2365 {
2366 	static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2367 	uint32* m;
2368 	float* na;
2369 	uint32* nb;
2370 	uint32 nc;
2371 	int o;
2372 	assert(sizeof(uint32)==4);
2373 	m=_TIFFmalloc(count*2*sizeof(uint32));
2374 	if (m==NULL)
2375 	{
2376 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2377 		return(0);
2378 	}
2379 	for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
2380 	{
2381 		if (*na<=0.0 || *na != *na)
2382 		{
2383 			nb[0]=0;
2384 			nb[1]=1;
2385 		}
2386 		else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
2387                          *na==(float)(uint32)(*na))
2388 		{
2389 			nb[0]=(uint32)(*na);
2390 			nb[1]=1;
2391 		}
2392 		else if (*na<1.0)
2393 		{
2394 			nb[0]=(uint32)((double)(*na)*0xFFFFFFFF);
2395 			nb[1]=0xFFFFFFFF;
2396 		}
2397 		else
2398 		{
2399 			nb[0]=0xFFFFFFFF;
2400 			nb[1]=(uint32)((double)0xFFFFFFFF/(*na));
2401 		}
2402 	}
2403 	if (tif->tif_flags&TIFF_SWAB)
2404 		TIFFSwabArrayOfLong(m,count*2);
2405 	o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
2406 	_TIFFfree(m);
2407 	return(o);
2408 }
2409 
2410 static int
2411 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
2412 {
2413 	static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2414 	int32* m;
2415 	float* na;
2416 	int32* nb;
2417 	uint32 nc;
2418 	int o;
2419 	assert(sizeof(int32)==4);
2420 	m=_TIFFmalloc(count*2*sizeof(int32));
2421 	if (m==NULL)
2422 	{
2423 		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2424 		return(0);
2425 	}
2426 	for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
2427 	{
2428 		if (*na<0.0)
2429 		{
2430 			if (*na==(int32)(*na))
2431 			{
2432 				nb[0]=(int32)(*na);
2433 				nb[1]=1;
2434 			}
2435 			else if (*na>-1.0)
2436 			{
2437 				nb[0]=-(int32)((double)(-*na)*0x7FFFFFFF);
2438 				nb[1]=0x7FFFFFFF;
2439 			}
2440 			else
2441 			{
2442 				nb[0]=-0x7FFFFFFF;
2443 				nb[1]=(int32)((double)0x7FFFFFFF/(-*na));
2444 			}
2445 		}
2446 		else
2447 		{
2448 			if (*na==(int32)(*na))
2449 			{
2450 				nb[0]=(int32)(*na);
2451 				nb[1]=1;
2452 			}
2453 			else if (*na<1.0)
2454 			{
2455 				nb[0]=(int32)((double)(*na)*0x7FFFFFFF);
2456 				nb[1]=0x7FFFFFFF;
2457 			}
2458 			else
2459 			{
2460 				nb[0]=0x7FFFFFFF;
2461 				nb[1]=(int32)((double)0x7FFFFFFF/(*na));
2462 			}
2463 		}
2464 	}
2465 	if (tif->tif_flags&TIFF_SWAB)
2466 		TIFFSwabArrayOfLong((uint32*)m,count*2);
2467 	o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
2468 	_TIFFfree(m);
2469 	return(o);
2470 }
2471 
2472 #ifdef notdef
2473 static int
2474 TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
2475 {
2476 	float m;
2477 	assert(sizeof(float)==4);
2478 	m=value;
2479 	TIFFCvtNativeToIEEEFloat(tif,1,&m);
2480 	if (tif->tif_flags&TIFF_SWAB)
2481 		TIFFSwabFloat(&m);
2482 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
2483 }
2484 #endif
2485 
2486 static int
2487 TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
2488 {
2489 	assert(count<0x40000000);
2490 	assert(sizeof(float)==4);
2491 	TIFFCvtNativeToIEEEFloat(tif,count,&value);
2492 	if (tif->tif_flags&TIFF_SWAB)
2493 		TIFFSwabArrayOfFloat(value,count);
2494 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,count,count*4,value));
2495 }
2496 
2497 #ifdef notdef
2498 static int
2499 TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
2500 {
2501 	double m;
2502 	assert(sizeof(double)==8);
2503 	m=value;
2504 	TIFFCvtNativeToIEEEDouble(tif,1,&m);
2505 	if (tif->tif_flags&TIFF_SWAB)
2506 		TIFFSwabDouble(&m);
2507 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
2508 }
2509 #endif
2510 
2511 static int
2512 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
2513 {
2514 	assert(count<0x20000000);
2515 	assert(sizeof(double)==8);
2516 	TIFFCvtNativeToIEEEDouble(tif,count,&value);
2517 	if (tif->tif_flags&TIFF_SWAB)
2518 		TIFFSwabArrayOfDouble(value,count);
2519 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,count,count*8,value));
2520 }
2521 
2522 static int
2523 TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
2524 {
2525 	assert(count<0x40000000);
2526 	assert(sizeof(uint32)==4);
2527 	if (tif->tif_flags&TIFF_SWAB)
2528 		TIFFSwabArrayOfLong(value,count);
2529 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));
2530 }
2531 
2532 static int
2533 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
2534 {
2535 	assert(count<0x20000000);
2536 	assert(sizeof(uint64)==8);
2537 	assert(tif->tif_flags&TIFF_BIGTIFF);
2538 	if (tif->tif_flags&TIFF_SWAB)
2539 		TIFFSwabArrayOfLong8(value,count);
2540 	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD8,count,count*8,value));
2541 }
2542 
2543 static int
2544 TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)
2545 {
2546 	static const char module[] = "TIFFWriteDirectoryTagData";
2547 	uint32 m;
2548 	m=0;
2549 	while (m<(*ndir))
2550 	{
2551 		assert(dir[m].tdir_tag!=tag);
2552 		if (dir[m].tdir_tag>tag)
2553 			break;
2554 		m++;
2555 	}
2556 	if (m<(*ndir))
2557 	{
2558 		uint32 n;
2559 		for (n=*ndir; n>m; n--)
2560 			dir[n]=dir[n-1];
2561 	}
2562 	dir[m].tdir_tag=tag;
2563 	dir[m].tdir_type=datatype;
2564 	dir[m].tdir_count=count;
2565 	dir[m].tdir_offset.toff_long8 = 0;
2566 	if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
2567         {
2568             if( data && datalength )
2569             {
2570                 _TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
2571             }
2572         }
2573 	else
2574 	{
2575 		uint64 na,nb;
2576 		na=tif->tif_dataoff;
2577 		nb=na+datalength;
2578 		if (!(tif->tif_flags&TIFF_BIGTIFF))
2579 			nb=(uint32)nb;
2580 		if ((nb<na)||(nb<datalength))
2581 		{
2582 			TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
2583 			return(0);
2584 		}
2585 		if (!SeekOK(tif,na))
2586 		{
2587 			TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
2588 			return(0);
2589 		}
2590 		assert(datalength<0x80000000UL);
2591 		if (!WriteOK(tif,data,(tmsize_t)datalength))
2592 		{
2593 			TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
2594 			return(0);
2595 		}
2596 		tif->tif_dataoff=nb;
2597 		if (tif->tif_dataoff&1)
2598 			tif->tif_dataoff++;
2599 		if (!(tif->tif_flags&TIFF_BIGTIFF))
2600 		{
2601 			uint32 o;
2602 			o=(uint32)na;
2603 			if (tif->tif_flags&TIFF_SWAB)
2604 				TIFFSwabLong(&o);
2605 			_TIFFmemcpy(&dir[m].tdir_offset,&o,4);
2606 		}
2607 		else
2608 		{
2609 			dir[m].tdir_offset.toff_long8 = na;
2610 			if (tif->tif_flags&TIFF_SWAB)
2611 				TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
2612 		}
2613 	}
2614 	(*ndir)++;
2615 	return(1);
2616 }
2617 
2618 /*
2619  * Link the current directory into the directory chain for the file.
2620  */
2621 static int
2622 TIFFLinkDirectory(TIFF* tif)
2623 {
2624 	static const char module[] = "TIFFLinkDirectory";
2625 
2626 	tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) & (~((toff_t)1));
2627 
2628 	/*
2629 	 * Handle SubIFDs
2630 	 */
2631 	if (tif->tif_flags & TIFF_INSUBIFD)
2632 	{
2633 		if (!(tif->tif_flags&TIFF_BIGTIFF))
2634 		{
2635 			uint32 m;
2636 			m = (uint32)tif->tif_diroff;
2637 			if (tif->tif_flags & TIFF_SWAB)
2638 				TIFFSwabLong(&m);
2639 			(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
2640 			if (!WriteOK(tif, &m, 4)) {
2641 				TIFFErrorExt(tif->tif_clientdata, module,
2642 				     "Error writing SubIFD directory link");
2643 				return (0);
2644 			}
2645 			/*
2646 			 * Advance to the next SubIFD or, if this is
2647 			 * the last one configured, revert back to the
2648 			 * normal directory linkage.
2649 			 */
2650 			if (--tif->tif_nsubifd)
2651 				tif->tif_subifdoff += 4;
2652 			else
2653 				tif->tif_flags &= ~TIFF_INSUBIFD;
2654 			return (1);
2655 		}
2656 		else
2657 		{
2658 			uint64 m;
2659 			m = tif->tif_diroff;
2660 			if (tif->tif_flags & TIFF_SWAB)
2661 				TIFFSwabLong8(&m);
2662 			(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
2663 			if (!WriteOK(tif, &m, 8)) {
2664 				TIFFErrorExt(tif->tif_clientdata, module,
2665 				     "Error writing SubIFD directory link");
2666 				return (0);
2667 			}
2668 			/*
2669 			 * Advance to the next SubIFD or, if this is
2670 			 * the last one configured, revert back to the
2671 			 * normal directory linkage.
2672 			 */
2673 			if (--tif->tif_nsubifd)
2674 				tif->tif_subifdoff += 8;
2675 			else
2676 				tif->tif_flags &= ~TIFF_INSUBIFD;
2677 			return (1);
2678 		}
2679 	}
2680 
2681 	if (!(tif->tif_flags&TIFF_BIGTIFF))
2682 	{
2683 		uint32 m;
2684 		uint32 nextdir;
2685 		m = (uint32)(tif->tif_diroff);
2686 		if (tif->tif_flags & TIFF_SWAB)
2687 			TIFFSwabLong(&m);
2688 		if (tif->tif_header.classic.tiff_diroff == 0) {
2689 			/*
2690 			 * First directory, overwrite offset in header.
2691 			 */
2692 			tif->tif_header.classic.tiff_diroff = (uint32) tif->tif_diroff;
2693 			(void) TIFFSeekFile(tif,4, SEEK_SET);
2694 			if (!WriteOK(tif, &m, 4)) {
2695 				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
2696 					     "Error writing TIFF header");
2697 				return (0);
2698 			}
2699 			return (1);
2700 		}
2701 		/*
2702 		 * Not the first directory, search to the last and append.
2703 		 */
2704 		nextdir = tif->tif_header.classic.tiff_diroff;
2705 		while(1) {
2706 			uint16 dircount;
2707 			uint32 nextnextdir;
2708 
2709 			if (!SeekOK(tif, nextdir) ||
2710 			    !ReadOK(tif, &dircount, 2)) {
2711 				TIFFErrorExt(tif->tif_clientdata, module,
2712 					     "Error fetching directory count");
2713 				return (0);
2714 			}
2715 			if (tif->tif_flags & TIFF_SWAB)
2716 				TIFFSwabShort(&dircount);
2717 			(void) TIFFSeekFile(tif,
2718 			    nextdir+2+dircount*12, SEEK_SET);
2719 			if (!ReadOK(tif, &nextnextdir, 4)) {
2720 				TIFFErrorExt(tif->tif_clientdata, module,
2721 					     "Error fetching directory link");
2722 				return (0);
2723 			}
2724 			if (tif->tif_flags & TIFF_SWAB)
2725 				TIFFSwabLong(&nextnextdir);
2726 			if (nextnextdir==0)
2727 			{
2728 				(void) TIFFSeekFile(tif,
2729 				    nextdir+2+dircount*12, SEEK_SET);
2730 				if (!WriteOK(tif, &m, 4)) {
2731 					TIFFErrorExt(tif->tif_clientdata, module,
2732 					     "Error writing directory link");
2733 					return (0);
2734 				}
2735 				break;
2736 			}
2737 			nextdir=nextnextdir;
2738 		}
2739 	}
2740 	else
2741 	{
2742 		uint64 m;
2743 		uint64 nextdir;
2744 		m = tif->tif_diroff;
2745 		if (tif->tif_flags & TIFF_SWAB)
2746 			TIFFSwabLong8(&m);
2747 		if (tif->tif_header.big.tiff_diroff == 0) {
2748 			/*
2749 			 * First directory, overwrite offset in header.
2750 			 */
2751 			tif->tif_header.big.tiff_diroff = tif->tif_diroff;
2752 			(void) TIFFSeekFile(tif,8, SEEK_SET);
2753 			if (!WriteOK(tif, &m, 8)) {
2754 				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
2755 					     "Error writing TIFF header");
2756 				return (0);
2757 			}
2758 			return (1);
2759 		}
2760 		/*
2761 		 * Not the first directory, search to the last and append.
2762 		 */
2763 		nextdir = tif->tif_header.big.tiff_diroff;
2764 		while(1) {
2765 			uint64 dircount64;
2766 			uint16 dircount;
2767 			uint64 nextnextdir;
2768 
2769 			if (!SeekOK(tif, nextdir) ||
2770 			    !ReadOK(tif, &dircount64, 8)) {
2771 				TIFFErrorExt(tif->tif_clientdata, module,
2772 					     "Error fetching directory count");
2773 				return (0);
2774 			}
2775 			if (tif->tif_flags & TIFF_SWAB)
2776 				TIFFSwabLong8(&dircount64);
2777 			if (dircount64>0xFFFF)
2778 			{
2779 				TIFFErrorExt(tif->tif_clientdata, module,
2780 					     "Sanity check on tag count failed, likely corrupt TIFF");
2781 				return (0);
2782 			}
2783 			dircount=(uint16)dircount64;
2784 			(void) TIFFSeekFile(tif,
2785 			    nextdir+8+dircount*20, SEEK_SET);
2786 			if (!ReadOK(tif, &nextnextdir, 8)) {
2787 				TIFFErrorExt(tif->tif_clientdata, module,
2788 					     "Error fetching directory link");
2789 				return (0);
2790 			}
2791 			if (tif->tif_flags & TIFF_SWAB)
2792 				TIFFSwabLong8(&nextnextdir);
2793 			if (nextnextdir==0)
2794 			{
2795 				(void) TIFFSeekFile(tif,
2796 				    nextdir+8+dircount*20, SEEK_SET);
2797 				if (!WriteOK(tif, &m, 8)) {
2798 					TIFFErrorExt(tif->tif_clientdata, module,
2799 					     "Error writing directory link");
2800 					return (0);
2801 				}
2802 				break;
2803 			}
2804 			nextdir=nextnextdir;
2805 		}
2806 	}
2807 	return (1);
2808 }
2809 
2810 /************************************************************************/
2811 /*                          TIFFRewriteField()                          */
2812 /*                                                                      */
2813 /*      Rewrite a field in the directory on disk without regard to      */
2814 /*      updating the TIFF directory structure in memory.  Currently     */
2815 /*      only supported for field that already exist in the on-disk      */
2816 /*      directory.  Mainly used for updating stripoffset /              */
2817 /*      stripbytecount values after the directory is already on         */
2818 /*      disk.                                                           */
2819 /*                                                                      */
2820 /*      Returns zero on failure, and one on success.                    */
2821 /************************************************************************/
2822 
2823 int
2824 _TIFFRewriteField(TIFF* tif, uint16 tag, TIFFDataType in_datatype,
2825                   tmsize_t count, void* data)
2826 {
2827     static const char module[] = "TIFFResetField";
2828     /* const TIFFField* fip = NULL; */
2829     uint16 dircount;
2830     tmsize_t dirsize;
2831     uint8 direntry_raw[20];
2832     uint16 entry_tag = 0;
2833     uint16 entry_type = 0;
2834     uint64 entry_count = 0;
2835     uint64 entry_offset = 0;
2836     int    value_in_entry = 0;
2837     uint64 read_offset;
2838     uint8 *buf_to_write = NULL;
2839     TIFFDataType datatype;
2840 
2841 /* -------------------------------------------------------------------- */
2842 /*      Find field definition.                                          */
2843 /* -------------------------------------------------------------------- */
2844     /*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
2845 
2846 /* -------------------------------------------------------------------- */
2847 /*      Do some checking this is a straight forward case.               */
2848 /* -------------------------------------------------------------------- */
2849     if( isMapped(tif) )
2850     {
2851         TIFFErrorExt( tif->tif_clientdata, module,
2852                       "Memory mapped files not currently supported for this operation." );
2853         return 0;
2854     }
2855 
2856     if( tif->tif_diroff == 0 )
2857     {
2858         TIFFErrorExt( tif->tif_clientdata, module,
2859                       "Attempt to reset field on directory not already on disk." );
2860         return 0;
2861     }
2862 
2863 /* -------------------------------------------------------------------- */
2864 /*      Read the directory entry count.                                 */
2865 /* -------------------------------------------------------------------- */
2866     if (!SeekOK(tif, tif->tif_diroff)) {
2867         TIFFErrorExt(tif->tif_clientdata, module,
2868                      "%s: Seek error accessing TIFF directory",
2869                      tif->tif_name);
2870         return 0;
2871     }
2872 
2873     read_offset = tif->tif_diroff;
2874 
2875     if (!(tif->tif_flags&TIFF_BIGTIFF))
2876     {
2877         if (!ReadOK(tif, &dircount, sizeof (uint16))) {
2878             TIFFErrorExt(tif->tif_clientdata, module,
2879                          "%s: Can not read TIFF directory count",
2880                          tif->tif_name);
2881             return 0;
2882         }
2883         if (tif->tif_flags & TIFF_SWAB)
2884             TIFFSwabShort(&dircount);
2885         dirsize = 12;
2886         read_offset += 2;
2887     } else {
2888         uint64 dircount64;
2889         if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
2890             TIFFErrorExt(tif->tif_clientdata, module,
2891                          "%s: Can not read TIFF directory count",
2892                          tif->tif_name);
2893             return 0;
2894         }
2895         if (tif->tif_flags & TIFF_SWAB)
2896             TIFFSwabLong8(&dircount64);
2897         dircount = (uint16)dircount64;
2898         dirsize = 20;
2899         read_offset += 8;
2900     }
2901 
2902 /* -------------------------------------------------------------------- */
2903 /*      Read through directory to find target tag.                      */
2904 /* -------------------------------------------------------------------- */
2905     while( dircount > 0 )
2906     {
2907         if (!ReadOK(tif, direntry_raw, dirsize)) {
2908             TIFFErrorExt(tif->tif_clientdata, module,
2909                          "%s: Can not read TIFF directory entry.",
2910                          tif->tif_name);
2911             return 0;
2912         }
2913 
2914         memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16) );
2915         if (tif->tif_flags&TIFF_SWAB)
2916             TIFFSwabShort( &entry_tag );
2917 
2918         if( entry_tag == tag )
2919             break;
2920 
2921         read_offset += dirsize;
2922     }
2923 
2924     if( entry_tag != tag )
2925     {
2926         TIFFErrorExt(tif->tif_clientdata, module,
2927                      "%s: Could not find tag %d.",
2928                      tif->tif_name, tag );
2929         return 0;
2930     }
2931 
2932 /* -------------------------------------------------------------------- */
2933 /*      Extract the type, count and offset for this entry.              */
2934 /* -------------------------------------------------------------------- */
2935     memcpy( &entry_type, direntry_raw + 2, sizeof(uint16) );
2936     if (tif->tif_flags&TIFF_SWAB)
2937         TIFFSwabShort( &entry_type );
2938 
2939     if (!(tif->tif_flags&TIFF_BIGTIFF))
2940     {
2941         uint32 value;
2942 
2943         memcpy( &value, direntry_raw + 4, sizeof(uint32) );
2944         if (tif->tif_flags&TIFF_SWAB)
2945             TIFFSwabLong( &value );
2946         entry_count = value;
2947 
2948         memcpy( &value, direntry_raw + 8, sizeof(uint32) );
2949         if (tif->tif_flags&TIFF_SWAB)
2950             TIFFSwabLong( &value );
2951         entry_offset = value;
2952     }
2953     else
2954     {
2955         memcpy( &entry_count, direntry_raw + 4, sizeof(uint64) );
2956         if (tif->tif_flags&TIFF_SWAB)
2957             TIFFSwabLong8( &entry_count );
2958 
2959         memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64) );
2960         if (tif->tif_flags&TIFF_SWAB)
2961             TIFFSwabLong8( &entry_offset );
2962     }
2963 
2964 /* -------------------------------------------------------------------- */
2965 /*      When a dummy tag was written due to TIFFDeferStrileArrayWriting() */
2966 /* -------------------------------------------------------------------- */
2967     if( entry_offset == 0 && entry_count == 0 && entry_type == 0 )
2968     {
2969         if( tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS )
2970         {
2971             entry_type = (tif->tif_flags&TIFF_BIGTIFF) ? TIFF_LONG8 : TIFF_LONG;
2972         }
2973         else
2974         {
2975             int write_aslong8 = 1;
2976             if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
2977             {
2978                 write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
2979             }
2980             else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
2981             {
2982                 write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
2983             }
2984             if( write_aslong8 )
2985             {
2986                 entry_type = TIFF_LONG8;
2987             }
2988             else
2989             {
2990                 int write_aslong4 = 1;
2991                 if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
2992                 {
2993                     write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
2994                 }
2995                 else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
2996                 {
2997                     write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
2998                 }
2999                 if( write_aslong4 )
3000                 {
3001                     entry_type = TIFF_LONG;
3002                 }
3003                 else
3004                 {
3005                     entry_type = TIFF_SHORT;
3006                 }
3007             }
3008         }
3009     }
3010 
3011 /* -------------------------------------------------------------------- */
3012 /*      What data type do we want to write this as?                     */
3013 /* -------------------------------------------------------------------- */
3014     if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
3015     {
3016         if( in_datatype == TIFF_LONG8 )
3017             datatype = entry_type == TIFF_SHORT ? TIFF_SHORT : TIFF_LONG;
3018         else if( in_datatype == TIFF_SLONG8 )
3019             datatype = TIFF_SLONG;
3020         else if( in_datatype == TIFF_IFD8 )
3021             datatype = TIFF_IFD;
3022         else
3023             datatype = in_datatype;
3024     }
3025     else
3026     {
3027         if( in_datatype == TIFF_LONG8 &&
3028             (entry_type == TIFF_SHORT || entry_type == TIFF_LONG ||
3029              entry_type == TIFF_LONG8 ) )
3030             datatype = entry_type;
3031         else if( in_datatype == TIFF_SLONG8 &&
3032             (entry_type == TIFF_SLONG || entry_type == TIFF_SLONG8 ) )
3033             datatype = entry_type;
3034         else if( in_datatype == TIFF_IFD8 &&
3035             (entry_type == TIFF_IFD || entry_type == TIFF_IFD8 ) )
3036             datatype = entry_type;
3037         else
3038             datatype = in_datatype;
3039     }
3040 
3041 /* -------------------------------------------------------------------- */
3042 /*      Prepare buffer of actual data to write.  This includes          */
3043 /*      swabbing as needed.                                             */
3044 /* -------------------------------------------------------------------- */
3045     buf_to_write =
3046 	    (uint8 *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
3047 				      "for field buffer.");
3048     if (!buf_to_write)
3049         return 0;
3050 
3051     if( datatype == in_datatype )
3052         memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
3053     else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
3054     {
3055 	tmsize_t i;
3056 
3057         for( i = 0; i < count; i++ )
3058         {
3059             ((int32 *) buf_to_write)[i] =
3060                 (int32) ((int64 *) data)[i];
3061             if( (int64) ((int32 *) buf_to_write)[i] != ((int64 *) data)[i] )
3062             {
3063                 _TIFFfree( buf_to_write );
3064                 TIFFErrorExt( tif->tif_clientdata, module,
3065                               "Value exceeds 32bit range of output type." );
3066                 return 0;
3067             }
3068         }
3069     }
3070     else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
3071              || (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
3072     {
3073 	tmsize_t i;
3074 
3075         for( i = 0; i < count; i++ )
3076         {
3077             ((uint32 *) buf_to_write)[i] =
3078                 (uint32) ((uint64 *) data)[i];
3079             if( (uint64) ((uint32 *) buf_to_write)[i] != ((uint64 *) data)[i] )
3080             {
3081                 _TIFFfree( buf_to_write );
3082                 TIFFErrorExt( tif->tif_clientdata, module,
3083                               "Value exceeds 32bit range of output type." );
3084                 return 0;
3085             }
3086         }
3087     }
3088     else if( datatype == TIFF_SHORT && in_datatype == TIFF_LONG8 )
3089     {
3090 	tmsize_t i;
3091 
3092         for( i = 0; i < count; i++ )
3093         {
3094             ((uint16 *) buf_to_write)[i] =
3095                 (uint16) ((uint64 *) data)[i];
3096             if( (uint64) ((uint16 *) buf_to_write)[i] != ((uint64 *) data)[i] )
3097             {
3098                 _TIFFfree( buf_to_write );
3099                 TIFFErrorExt( tif->tif_clientdata, module,
3100                               "Value exceeds 16bit range of output type." );
3101                 return 0;
3102             }
3103         }
3104     }
3105     else
3106     {
3107         TIFFErrorExt( tif->tif_clientdata, module,
3108                       "Unhandled type conversion." );
3109         return 0;
3110     }
3111 
3112     if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
3113     {
3114         if( TIFFDataWidth(datatype) == 2 )
3115             TIFFSwabArrayOfShort( (uint16 *) buf_to_write, count );
3116         else if( TIFFDataWidth(datatype) == 4 )
3117             TIFFSwabArrayOfLong( (uint32 *) buf_to_write, count );
3118         else if( TIFFDataWidth(datatype) == 8 )
3119             TIFFSwabArrayOfLong8( (uint64 *) buf_to_write, count );
3120     }
3121 
3122 /* -------------------------------------------------------------------- */
3123 /*      Is this a value that fits into the directory entry?             */
3124 /* -------------------------------------------------------------------- */
3125     if (!(tif->tif_flags&TIFF_BIGTIFF))
3126     {
3127         if( TIFFDataWidth(datatype) * count <= 4 )
3128         {
3129             entry_offset = read_offset + 8;
3130             value_in_entry = 1;
3131         }
3132     }
3133     else
3134     {
3135         if( TIFFDataWidth(datatype) * count <= 8 )
3136         {
3137             entry_offset = read_offset + 12;
3138             value_in_entry = 1;
3139         }
3140     }
3141 
3142     if( (tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS) &&
3143         tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
3144         tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
3145         tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 )
3146     {
3147         tif->tif_dir.td_stripoffset_entry.tdir_type = datatype;
3148         tif->tif_dir.td_stripoffset_entry.tdir_count = count;
3149     }
3150     else if( (tag == TIFFTAG_TILEBYTECOUNTS || tag == TIFFTAG_STRIPBYTECOUNTS) &&
3151         tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
3152         tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
3153         tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )
3154     {
3155         tif->tif_dir.td_stripbytecount_entry.tdir_type = datatype;
3156         tif->tif_dir.td_stripbytecount_entry.tdir_count = count;
3157     }
3158 
3159 /* -------------------------------------------------------------------- */
3160 /*      If the tag type, and count match, then we just write it out     */
3161 /*      over the old values without altering the directory entry at     */
3162 /*      all.                                                            */
3163 /* -------------------------------------------------------------------- */
3164     if( entry_count == (uint64)count && entry_type == (uint16) datatype )
3165     {
3166         if (!SeekOK(tif, entry_offset)) {
3167             _TIFFfree( buf_to_write );
3168             TIFFErrorExt(tif->tif_clientdata, module,
3169                          "%s: Seek error accessing TIFF directory",
3170                          tif->tif_name);
3171             return 0;
3172         }
3173         if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
3174             _TIFFfree( buf_to_write );
3175             TIFFErrorExt(tif->tif_clientdata, module,
3176                          "Error writing directory link");
3177             return (0);
3178         }
3179 
3180         _TIFFfree( buf_to_write );
3181         return 1;
3182     }
3183 
3184 /* -------------------------------------------------------------------- */
3185 /*      Otherwise, we write the new tag data at the end of the file.    */
3186 /* -------------------------------------------------------------------- */
3187     if( !value_in_entry )
3188     {
3189         entry_offset = TIFFSeekFile(tif,0,SEEK_END);
3190 
3191         if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
3192             _TIFFfree( buf_to_write );
3193             TIFFErrorExt(tif->tif_clientdata, module,
3194                          "Error writing directory link");
3195             return (0);
3196         }
3197     }
3198     else
3199     {
3200         memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
3201     }
3202 
3203     _TIFFfree( buf_to_write );
3204     buf_to_write = 0;
3205 
3206 /* -------------------------------------------------------------------- */
3207 /*      Adjust the directory entry.                                     */
3208 /* -------------------------------------------------------------------- */
3209     entry_type = datatype;
3210     entry_count = (uint64)count;
3211     memcpy( direntry_raw + 2, &entry_type, sizeof(uint16) );
3212     if (tif->tif_flags&TIFF_SWAB)
3213         TIFFSwabShort( (uint16 *) (direntry_raw + 2) );
3214 
3215     if (!(tif->tif_flags&TIFF_BIGTIFF))
3216     {
3217         uint32 value;
3218 
3219         value = (uint32) entry_count;
3220         memcpy( direntry_raw + 4, &value, sizeof(uint32) );
3221         if (tif->tif_flags&TIFF_SWAB)
3222             TIFFSwabLong( (uint32 *) (direntry_raw + 4) );
3223 
3224         value = (uint32) entry_offset;
3225         memcpy( direntry_raw + 8, &value, sizeof(uint32) );
3226         if (tif->tif_flags&TIFF_SWAB)
3227             TIFFSwabLong( (uint32 *) (direntry_raw + 8) );
3228     }
3229     else
3230     {
3231         memcpy( direntry_raw + 4, &entry_count, sizeof(uint64) );
3232         if (tif->tif_flags&TIFF_SWAB)
3233             TIFFSwabLong8( (uint64 *) (direntry_raw + 4) );
3234 
3235         memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64) );
3236         if (tif->tif_flags&TIFF_SWAB)
3237             TIFFSwabLong8( (uint64 *) (direntry_raw + 12) );
3238     }
3239 
3240 /* -------------------------------------------------------------------- */
3241 /*      Write the directory entry out to disk.                          */
3242 /* -------------------------------------------------------------------- */
3243     if (!SeekOK(tif, read_offset )) {
3244         TIFFErrorExt(tif->tif_clientdata, module,
3245                      "%s: Seek error accessing TIFF directory",
3246                      tif->tif_name);
3247         return 0;
3248     }
3249 
3250     if (!WriteOK(tif, direntry_raw,dirsize))
3251     {
3252         TIFFErrorExt(tif->tif_clientdata, module,
3253                      "%s: Can not write TIFF directory entry.",
3254                      tif->tif_name);
3255         return 0;
3256     }
3257 
3258     return 1;
3259 }
3260 /* vim: set ts=8 sts=8 sw=8 noet: */
3261 /*
3262  * Local Variables:
3263  * mode: c
3264  * c-basic-offset: 8
3265  * fill-column: 78
3266  * End:
3267  */
3268