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