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