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