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