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