1/* PDFlib GmbH cvsid: $Id: tif_dirread.c 14574 2005-10-29 16:27:43Z bonefish $ */ 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 Read Support Routines. 31 */ 32#include "tiffiop.h" 33 34#define IGNORE 0 /* tag placeholder used below */ 35 36#if HAVE_IEEEFP 37#define TIFFCvtIEEEFloatToNative(tif, n, fp) 38#define TIFFCvtIEEEDoubleToNative(tif, n, dp) 39#else 40extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*); 41extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*); 42#endif 43 44static void EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16); 45static void MissingRequired(TIFF*, const char*); 46static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32); 47static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*); 48static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*); 49static float TIFFFetchRational(TIFF*, TIFFDirEntry*); 50static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*); 51static int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, int*); 52static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*); 53static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*); 54static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**); 55static int TIFFFetchExtraSamples(TIFF*, TIFFDirEntry*); 56static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*); 57static float TIFFFetchFloat(TIFF*, TIFFDirEntry*); 58static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*); 59static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*); 60static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*); 61static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*); 62static void ChopUpSingleUncompressedStrip(TIFF*); 63 64static char * 65CheckMalloc(TIFF* tif, tsize_t n, const char* what) 66{ 67 char *cp = (char*)_TIFFmalloc(tif, n); 68 if (cp == NULL) 69 TIFFError(tif->tif_name, "No space %s", what); 70 return (cp); 71} 72 73/* 74 * Read the next TIFF directory from a file 75 * and convert it to the internal format. 76 * We read directories sequentially. 77 */ 78int 79TIFFReadDirectory(TIFF* tif) 80{ 81 register TIFFDirEntry* dp; 82 register int n; 83 register TIFFDirectory* td; 84 TIFFDirEntry* dir; 85 int iv; 86 long v; 87 double dv; 88 const TIFFFieldInfo* fip; 89 int fix; 90 uint16 dircount; 91 toff_t nextdiroff; 92 char* cp; 93 int diroutoforderwarning = 0; 94 95 tif->tif_diroff = tif->tif_nextdiroff; 96 if (tif->tif_diroff == 0) /* no more directories */ 97 return (0); 98 /* 99 * Cleanup any previous compression state. 100 */ 101 (*tif->tif_cleanup)(tif); 102 tif->tif_curdir++; 103 nextdiroff = 0; 104 if (!isMapped(tif)) { 105 if (!SeekOK(tif, tif->tif_diroff)) { 106 TIFFError(tif->tif_name, 107 "Seek error accessing TIFF directory"); 108 return (0); 109 } 110 if (!ReadOK(tif, &dircount, sizeof (uint16))) { 111 TIFFError(tif->tif_name, 112 "Can not read TIFF directory count"); 113 return (0); 114 } 115 116 /* 117 * PDFlib GmbH: EFAX (*.jfx) files have a dircount of 0, 118 * and cannot be processed by TIFFlib. 119 */ 120 if (dircount == 0) 121 return (0); 122 123 if (tif->tif_flags & TIFF_SWAB) 124 TIFFSwabShort(&dircount); 125 dir = (TIFFDirEntry *)CheckMalloc(tif, 126 dircount * sizeof (TIFFDirEntry), "to read TIFF directory"); 127 if (dir == NULL) 128 return (0); 129 if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) { 130 TIFFError(tif->tif_name, "Can not read TIFF directory"); 131 goto bad; 132 } 133 /* 134 * Read offset to next directory for sequential scans. 135 */ 136 (void) ReadOK(tif, &nextdiroff, ((tsize_t) sizeof (uint32))); 137 } else { 138 toff_t off = tif->tif_diroff; 139 140 if (off + sizeof (uint16) > tif->tif_size) { 141 TIFFError(tif->tif_name, 142 "Can not read TIFF directory count"); 143 return (0); 144 } else 145 _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof(uint16)); 146 off += sizeof (uint16); 147 if (tif->tif_flags & TIFF_SWAB) 148 TIFFSwabShort(&dircount); 149 dir = (TIFFDirEntry *)CheckMalloc(tif, 150 dircount * sizeof (TIFFDirEntry), "to read TIFF directory"); 151 if (dir == NULL) 152 return (0); 153 if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) { 154 TIFFError(tif->tif_name, "Can not read TIFF directory"); 155 goto bad; 156 } else 157 _TIFFmemcpy(dir, tif->tif_base + off, 158 dircount*sizeof (TIFFDirEntry)); 159 off += dircount* sizeof (TIFFDirEntry); 160 if (off + sizeof (uint32) <= tif->tif_size) 161 _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof(uint32)); 162 } 163 if (tif->tif_flags & TIFF_SWAB) 164 TIFFSwabLong(&nextdiroff); 165 tif->tif_nextdiroff = nextdiroff; 166 167 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */ 168 /* 169 * Setup default value and then make a pass over 170 * the fields to check type and tag information, 171 * and to extract info required to size data 172 * structures. A second pass is made afterwards 173 * to read in everthing not taken in the first pass. 174 */ 175 td = &tif->tif_dir; 176 /* free any old stuff and reinit */ 177 TIFFFreeDirectory(tif); 178 TIFFDefaultDirectory(tif); 179 /* 180 * Electronic Arts writes gray-scale TIFF files 181 * without a PlanarConfiguration directory entry. 182 * Thus we setup a default value here, even though 183 * the TIFF spec says there is no default value. 184 */ 185 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 186 187 /* 188 * Sigh, we must make a separate pass through the 189 * directory for the following reason: 190 * 191 * We must process the Compression tag in the first pass 192 * in order to merge in codec-private tag definitions (otherwise 193 * we may get complaints about unknown tags). However, the 194 * Compression tag may be dependent on the SamplesPerPixel 195 * tag value because older TIFF specs permited Compression 196 * to be written as a SamplesPerPixel-count tag entry. 197 * Thus if we don't first figure out the correct SamplesPerPixel 198 * tag value then we may end up ignoring the Compression tag 199 * value because it has an incorrect count value (if the 200 * true value of SamplesPerPixel is not 1). 201 * 202 * It sure would have been nice if Aldus had really thought 203 * this stuff through carefully. 204 */ 205 for (dp = dir, n = dircount; n > 0; n--, dp++) { 206 if (tif->tif_flags & TIFF_SWAB) { 207 TIFFSwabArrayOfShort(&dp->tdir_tag, 2); 208 TIFFSwabArrayOfLong(&dp->tdir_count, 2); 209 } 210 if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) { 211 if (!TIFFFetchNormalTag(tif, dp)) 212 goto bad; 213 dp->tdir_tag = IGNORE; 214 } 215 } 216 /* 217 * First real pass over the directory. 218 */ 219 fix = 0; 220 for (dp = dir, n = dircount; n > 0; n--, dp++) { 221 222 /* 223 * Find the field information entry for this tag. 224 * Added check for tags to ignore ... [BFC] 225 */ 226 if( TIFFReassignTagToIgnore(TIS_EXTRACT, dp->tdir_tag) ) 227 dp->tdir_tag = IGNORE; 228 229 if (dp->tdir_tag == IGNORE) 230 continue; 231 232 /* 233 * Silicon Beach (at least) writes unordered 234 * directory tags (violating the spec). Handle 235 * it here, but be obnoxious (maybe they'll fix it?). 236 */ 237 if (dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag) { 238 if (!diroutoforderwarning) { 239 TIFFWarning(tif->tif_name, 240 "invalid TIFF directory; tags are not sorted in ascending order"); 241 diroutoforderwarning = 1; 242 } 243 fix = 0; /* O(n^2) */ 244 } 245 while (fix < tif->tif_nfields && 246 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag) 247 fix++; 248 if (fix == tif->tif_nfields || 249 tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) { 250 TIFFWarning(tif->tif_name, 251 "unknown field with tag %d (0x%x) ignored", 252 dp->tdir_tag, dp->tdir_tag); 253 dp->tdir_tag = IGNORE; 254 fix = 0; /* restart search */ 255 continue; 256 } 257 /* 258 * Null out old tags that we ignore. 259 */ 260 if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) { 261 ignore: 262 dp->tdir_tag = IGNORE; 263 continue; 264 } 265 /* 266 * Check data type. 267 */ 268 fip = tif->tif_fieldinfo[fix]; 269 while (dp->tdir_type != (tif_short) fip->field_type) { 270 if (fip->field_type == TIFF_ANY) /* wildcard */ 271 break; 272 fip++, fix++; 273 if (fix == tif->tif_nfields || 274 fip->field_tag != dp->tdir_tag) { 275 TIFFWarning(tif->tif_name, 276 "wrong data type %d for \"%s\"; tag ignored", 277 dp->tdir_type, fip[-1].field_name); 278 goto ignore; 279 } 280 } 281 /* 282 * Check count if known in advance. 283 */ 284 if (fip->field_readcount != TIFF_VARIABLE) { 285 uint32 expected = (fip->field_readcount == TIFF_SPP) ? 286 (uint32) td->td_samplesperpixel : 287 (uint32) fip->field_readcount; 288 if (!CheckDirCount(tif, dp, expected)) 289 goto ignore; 290 } 291 292 switch (dp->tdir_tag) { 293 case TIFFTAG_COMPRESSION: 294 /* 295 * The 5.0 spec says the Compression tag has 296 * one value, while earlier specs say it has 297 * one value per sample. Because of this, we 298 * accept the tag if one value is supplied. 299 */ 300 if (dp->tdir_count == 1) { 301 v = TIFFExtractData(tif, 302 dp->tdir_type, dp->tdir_offset); 303 if (!TIFFSetField(tif, dp->tdir_tag, (int)v)) 304 goto bad; 305 break; 306 } 307 if (!TIFFFetchPerSampleShorts(tif, dp, &iv) || 308 !TIFFSetField(tif, dp->tdir_tag, iv)) 309 goto bad; 310 dp->tdir_tag = IGNORE; 311 break; 312 case TIFFTAG_STRIPOFFSETS: 313 case TIFFTAG_STRIPBYTECOUNTS: 314 case TIFFTAG_TILEOFFSETS: 315 case TIFFTAG_TILEBYTECOUNTS: 316 TIFFSetFieldBit(tif, fip->field_bit); 317 break; 318 case TIFFTAG_IMAGEWIDTH: 319 case TIFFTAG_IMAGELENGTH: 320 case TIFFTAG_IMAGEDEPTH: 321 case TIFFTAG_TILELENGTH: 322 case TIFFTAG_TILEWIDTH: 323 case TIFFTAG_TILEDEPTH: 324 case TIFFTAG_PLANARCONFIG: 325 case TIFFTAG_ROWSPERSTRIP: 326 if (!TIFFFetchNormalTag(tif, dp)) 327 goto bad; 328 dp->tdir_tag = IGNORE; 329 break; 330 case TIFFTAG_EXTRASAMPLES: 331 (void) TIFFFetchExtraSamples(tif, dp); 332 dp->tdir_tag = IGNORE; 333 break; 334 } 335 } 336 337 /* 338 * Allocate directory structure and setup defaults. 339 */ 340 if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) { 341 MissingRequired(tif, "ImageLength"); 342 goto bad; 343 } 344 if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) { 345 MissingRequired(tif, "PlanarConfiguration"); 346 goto bad; 347 } 348 /* 349 * Setup appropriate structures (by strip or by tile) 350 */ 351 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) { 352 td->td_nstrips = TIFFNumberOfStrips(tif); 353 td->td_tilewidth = td->td_imagewidth; 354 td->td_tilelength = td->td_rowsperstrip; 355 td->td_tiledepth = td->td_imagedepth; 356 tif->tif_flags &= ~TIFF_ISTILED; 357 } else { 358 td->td_nstrips = TIFFNumberOfTiles(tif); 359 tif->tif_flags |= TIFF_ISTILED; 360 } 361 td->td_stripsperimage = td->td_nstrips; 362 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 363 td->td_stripsperimage /= td->td_samplesperpixel; 364 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) { 365 MissingRequired(tif, 366 isTiled(tif) ? "TileOffsets" : "StripOffsets"); 367 goto bad; 368 } 369 370 /* 371 * Second pass: extract other information. 372 */ 373 for (dp = dir, n = dircount; n > 0; n--, dp++) { 374 if (dp->tdir_tag == IGNORE) 375 continue; 376 switch (dp->tdir_tag) { 377 case TIFFTAG_MINSAMPLEVALUE: 378 case TIFFTAG_MAXSAMPLEVALUE: 379 case TIFFTAG_BITSPERSAMPLE: 380 /* 381 * The 5.0 spec says the Compression tag has 382 * one value, while earlier specs say it has 383 * one value per sample. Because of this, we 384 * accept the tag if one value is supplied. 385 * 386 * The MinSampleValue, MaxSampleValue and 387 * BitsPerSample tags are supposed to be written 388 * as one value/sample, but some vendors incorrectly 389 * write one value only -- so we accept that 390 * as well (yech). 391 */ 392 if (dp->tdir_count == 1) { 393 v = TIFFExtractData(tif, 394 dp->tdir_type, dp->tdir_offset); 395 if (!TIFFSetField(tif, dp->tdir_tag, (int)v)) 396 goto bad; 397 break; 398 } 399 /* fall thru... */ 400 case TIFFTAG_DATATYPE: 401 case TIFFTAG_SAMPLEFORMAT: 402 if (!TIFFFetchPerSampleShorts(tif, dp, &iv) || 403 !TIFFSetField(tif, dp->tdir_tag, iv)) 404 goto bad; 405 break; 406 case TIFFTAG_SMINSAMPLEVALUE: 407 case TIFFTAG_SMAXSAMPLEVALUE: 408 if (!TIFFFetchPerSampleAnys(tif, dp, &dv) || 409 !TIFFSetField(tif, dp->tdir_tag, dv)) 410 goto bad; 411 break; 412 case TIFFTAG_STRIPOFFSETS: 413 case TIFFTAG_TILEOFFSETS: 414 if (!TIFFFetchStripThing(tif, dp, 415 td->td_nstrips, &td->td_stripoffset)) 416 goto bad; 417 break; 418 case TIFFTAG_STRIPBYTECOUNTS: 419 case TIFFTAG_TILEBYTECOUNTS: 420 if (!TIFFFetchStripThing(tif, dp, 421 td->td_nstrips, &td->td_stripbytecount)) 422 goto bad; 423 break; 424 case TIFFTAG_COLORMAP: 425 case TIFFTAG_TRANSFERFUNCTION: 426 /* 427 * TransferFunction can have either 1x or 3x data 428 * values; Colormap can have only 3x items. 429 */ 430 v = 1L<<td->td_bitspersample; 431 if (dp->tdir_tag == TIFFTAG_COLORMAP || 432 dp->tdir_count != (uint32) v) { 433 if (!CheckDirCount(tif, dp, (uint32)(3*v))) 434 break; 435 } 436 v *= sizeof (uint16); 437 cp = CheckMalloc(tif, dp->tdir_count * sizeof (uint16), 438 "to read \"TransferFunction\" tag"); 439 if (cp != NULL) { 440 if (TIFFFetchData(tif, dp, cp)) { 441 /* 442 * This deals with there being only 443 * one array to apply to all samples. 444 */ 445 uint32 c = 446 (uint32)1 << td->td_bitspersample; 447 if (dp->tdir_count == c) 448 v = 0; 449 TIFFSetField(tif, dp->tdir_tag, 450 cp, cp+v, cp+2*v); 451 } 452 _TIFFfree(tif, cp); 453 } 454 break; 455 case TIFFTAG_PAGENUMBER: 456 case TIFFTAG_HALFTONEHINTS: 457 case TIFFTAG_YCBCRSUBSAMPLING: 458 case TIFFTAG_DOTRANGE: 459 (void) TIFFFetchShortPair(tif, dp); 460 break; 461#ifdef COLORIMETRY_SUPPORT 462 case TIFFTAG_REFERENCEBLACKWHITE: 463 (void) TIFFFetchRefBlackWhite(tif, dp); 464 break; 465#endif 466/* BEGIN REV 4.0 COMPATIBILITY */ 467 case TIFFTAG_OSUBFILETYPE: 468 v = 0; 469 switch (TIFFExtractData(tif, dp->tdir_type, 470 dp->tdir_offset)) { 471 case OFILETYPE_REDUCEDIMAGE: 472 v = FILETYPE_REDUCEDIMAGE; 473 break; 474 case OFILETYPE_PAGE: 475 v = FILETYPE_PAGE; 476 break; 477 } 478 if (v) 479 (void) TIFFSetField(tif, 480 TIFFTAG_SUBFILETYPE, (int)v); 481 break; 482/* END REV 4.0 COMPATIBILITY */ 483 default: 484 (void) TIFFFetchNormalTag(tif, dp); 485 break; 486 } 487 } 488 /* 489 * Verify Palette image has a Colormap. 490 */ 491 if (td->td_photometric == PHOTOMETRIC_PALETTE && 492 !TIFFFieldSet(tif, FIELD_COLORMAP)) { 493 MissingRequired(tif, "Colormap"); 494 goto bad; 495 } 496 /* 497 * Attempt to deal with a missing StripByteCounts tag. 498 */ 499 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) { 500 /* 501 * Some manufacturers violate the spec by not giving 502 * the size of the strips. In this case, assume there 503 * is one uncompressed strip of data. 504 */ 505 if ((td->td_planarconfig == PLANARCONFIG_CONTIG && 506 td->td_nstrips > 1) || 507 (td->td_planarconfig == PLANARCONFIG_SEPARATE && 508 td->td_nstrips != td->td_samplesperpixel)) { 509 MissingRequired(tif, "StripByteCounts"); 510 goto bad; 511 } 512 TIFFWarning(tif->tif_name, 513"TIFF directory is missing required \"%s\" field, calculating from imagelength", 514 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name); 515 EstimateStripByteCounts(tif, dir, dircount); 516#define BYTECOUNTLOOKSBAD \ 517 ((td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \ 518 (td->td_compression == COMPRESSION_NONE && \ 519 td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0])) 520 } else if (td->td_nstrips == 1 && BYTECOUNTLOOKSBAD) { 521 /* 522 * Plexus (and others) sometimes give a value 523 * of zero for a tag when they don't know what 524 * the correct value is! Try and handle the 525 * simple case of estimating the size of a one 526 * strip image. 527 */ 528 TIFFWarning(tif->tif_name, 529 "Bogus \"%s\" field, ignoring and calculating from imagelength", 530 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name); 531 EstimateStripByteCounts(tif, dir, dircount); 532 } 533 if (dir) 534 _TIFFfree(tif, (char *)dir); 535 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) 536 td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1); 537 /* 538 * Setup default compression scheme. 539 */ 540 if (!TIFFFieldSet(tif, FIELD_COMPRESSION)) 541 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); 542 /* 543 * Some manufacturers make life difficult by writing 544 * large amounts of uncompressed data as a single strip. 545 * This is contrary to the recommendations of the spec. 546 * The following makes an attempt at breaking such images 547 * into strips closer to the recommended 8k bytes. A 548 * side effect, however, is that the RowsPerStrip tag 549 * value may be changed. 550 */ 551 if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE && 552 (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP) 553 ChopUpSingleUncompressedStrip(tif); 554 /* 555 * Reinitialize i/o since we are starting on a new directory. 556 */ 557 tif->tif_row = (uint32) -1; 558 tif->tif_curstrip = (tstrip_t) -1; 559 tif->tif_col = (uint32) -1; 560 tif->tif_curtile = (ttile_t) -1; 561 tif->tif_tilesize = TIFFTileSize(tif); 562 tif->tif_scanlinesize = TIFFScanlineSize(tif); 563 return (1); 564bad: 565 if (dir) 566 _TIFFfree(tif, dir); 567 return (0); 568} 569 570static void 571EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount) 572{ 573 register TIFFDirEntry *dp; 574 register TIFFDirectory *td = &tif->tif_dir; 575 uint16 i; 576 577 if (td->td_stripbytecount) 578 _TIFFfree(tif, td->td_stripbytecount); 579 td->td_stripbytecount = (uint32*) 580 CheckMalloc(tif, td->td_nstrips * sizeof (uint32), 581 "for \"StripByteCounts\" array"); 582 if (td->td_compression != COMPRESSION_NONE) { 583 uint32 space = (uint32)(sizeof (TIFFHeader) 584 + sizeof (uint16) 585 + (dircount * sizeof (TIFFDirEntry)) 586 + sizeof (uint32)); 587 toff_t filesize = TIFFGetFileSize(tif); 588 uint16 n; 589 590 /* calculate amount of space used by indirect values */ 591 for (dp = dir, n = dircount; n > 0; n--, dp++) { 592 uint32 cc = dp->tdir_count*tiffDataWidth[dp->tdir_type]; 593 if (cc > sizeof (uint32)) 594 space += cc; 595 } 596 space = filesize - space; 597 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) 598 space /= td->td_samplesperpixel; 599 for (i = 0; i < td->td_nstrips; i++) 600 td->td_stripbytecount[i] = space; 601 /* 602 * This gross hack handles the case were the offset to 603 * the last strip is past the place where we think the strip 604 * should begin. Since a strip of data must be contiguous, 605 * it's safe to assume that we've overestimated the amount 606 * of data in the strip and trim this number back accordingly. 607 */ 608 i--; 609 if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i])) 610 > filesize) 611 td->td_stripbytecount[i] = 612 filesize - td->td_stripoffset[i]; 613 } else { 614 uint32 rowbytes = TIFFScanlineSize(tif); 615 uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage; 616 for (i = 0; i < td->td_nstrips; i++) 617 td->td_stripbytecount[i] = rowbytes*rowsperstrip; 618 } 619 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); 620 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) 621 td->td_rowsperstrip = td->td_imagelength; 622} 623 624static void 625MissingRequired(TIFF* tif, const char* tagname) 626{ 627 TIFFError(tif->tif_name, 628 "TIFF directory is missing required \"%s\" field", tagname); 629} 630 631/* 632 * Check the count field of a directory 633 * entry against a known value. The caller 634 * is expected to skip/ignore the tag if 635 * there is a mismatch. 636 */ 637static int 638CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count) 639{ 640 if (count != dir->tdir_count) { 641 TIFFWarning(tif->tif_name, 642 "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored", 643 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, 644 dir->tdir_count, count); 645 return (0); 646 } 647 return (1); 648} 649 650/* 651 * Fetch a contiguous directory item. 652 */ 653static tsize_t 654TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp) 655{ 656 int w = tiffDataWidth[dir->tdir_type]; 657 tsize_t cc = dir->tdir_count * w; 658 659 if (!isMapped(tif)) { 660 if (!SeekOK(tif, dir->tdir_offset)) 661 goto bad; 662 if (!ReadOK(tif, cp, cc)) 663 goto bad; 664 } else { 665 if (dir->tdir_offset + cc > tif->tif_size) 666 goto bad; 667 _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc); 668 } 669 if (tif->tif_flags & TIFF_SWAB) { 670 switch (dir->tdir_type) { 671 case TIFF_SHORT: 672 case TIFF_SSHORT: 673 TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count); 674 break; 675 case TIFF_LONG: 676 case TIFF_SLONG: 677 case TIFF_FLOAT: 678 TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count); 679 break; 680 case TIFF_RATIONAL: 681 case TIFF_SRATIONAL: 682 TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count); 683 break; 684 case TIFF_DOUBLE: 685 TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count); 686 break; 687 } 688 } 689 return (cc); 690bad: 691 TIFFError(tif->tif_name, "Error fetching data for field \"%s\"", 692 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); 693 return ((tsize_t) 0); 694} 695 696/* 697 * Fetch an ASCII item from the file. 698 */ 699static tsize_t 700TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp) 701{ 702 if (dir->tdir_count <= 4) { 703 uint32 l = dir->tdir_offset; 704 if (tif->tif_flags & TIFF_SWAB) 705 TIFFSwabLong(&l); 706 _TIFFmemcpy(cp, &l, dir->tdir_count); 707 return (1); 708 } 709 return (TIFFFetchData(tif, dir, cp)); 710} 711 712/* 713 * Convert numerator+denominator to float. 714 */ 715static int 716cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv) 717{ 718 if (denom == 0) { 719 TIFFError(tif->tif_name, 720 "%s: Rational with zero denominator (num = %lu)", 721 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num); 722 return (0); 723 } else { 724 if (dir->tdir_type == TIFF_RATIONAL) 725 *rv = ((float)num / (float)denom); 726 else 727 *rv = ((float)(int32)num / (float)(int32)denom); 728 return (1); 729 } 730} 731 732/* 733 * Fetch a rational item from the file 734 * at offset off and return the value 735 * as a floating point number. 736 */ 737static float 738TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir) 739{ 740 uint32 l[2]; 741 float v; 742 743 return (!TIFFFetchData(tif, dir, (char *)l) || 744 !cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v); 745} 746 747/* 748 * Fetch a single floating point value 749 * from the offset field and return it 750 * as a native float. 751 */ 752static float 753TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir) 754{ 755 long l = TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset); 756 float v = *(float*) &l; 757 TIFFCvtIEEEFloatToNative(tif, 1, &v); 758 return (v); 759} 760 761/* 762 * Fetch an array of BYTE or SBYTE values. 763 */ 764static int 765TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v) 766{ 767 if (dir->tdir_count <= 4) { 768 /* 769 * Extract data from offset field. 770 */ 771 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) { 772 switch (dir->tdir_count) { 773 case 4: v[3] = (uint16)(dir->tdir_offset & 0xff); 774 case 3: v[2] = (uint16)((dir->tdir_offset >> 8) & 0xff); 775 case 2: v[1] = (uint16)((dir->tdir_offset >> 16) & 0xff); 776 case 1: v[0] = (uint16)(dir->tdir_offset >> 24); 777 } 778 } else { 779 switch (dir->tdir_count) { 780 case 4: v[3] = (uint16)(dir->tdir_offset >> 24); 781 case 3: v[2] = (uint16)((dir->tdir_offset >> 16) & 0xff); 782 case 2: v[1] = (uint16)((dir->tdir_offset >> 8) & 0xff); 783 case 1: v[0] = (uint16)(dir->tdir_offset & 0xff); 784 } 785 } 786 return (1); 787 } else 788 return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */ 789} 790 791/* 792 * Fetch an array of SHORT or SSHORT values. 793 */ 794static int 795TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v) 796{ 797 if (dir->tdir_count <= 2) { 798 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) { 799 switch (dir->tdir_count) { 800 case 2: v[1] = (uint16) (dir->tdir_offset & 0xffff); 801 case 1: v[0] = (uint16) (dir->tdir_offset >> 16); 802 } 803 } else { 804 switch (dir->tdir_count) { 805 case 2: v[1] = (uint16) (dir->tdir_offset >> 16); 806 case 1: v[0] = (uint16) (dir->tdir_offset & 0xffff); 807 } 808 } 809 return (1); 810 } else 811 return (TIFFFetchData(tif, dir, (char *)v) != 0); 812} 813 814/* 815 * Fetch a pair of SHORT or BYTE values. 816 */ 817static int 818TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir) 819{ 820 uint16 v[2]; 821 int ok = 0; 822 823 switch (dir->tdir_type) { 824 case TIFF_SHORT: 825 case TIFF_SSHORT: 826 ok = TIFFFetchShortArray(tif, dir, v); 827 break; 828 case TIFF_BYTE: 829 case TIFF_SBYTE: 830 ok = TIFFFetchByteArray(tif, dir, v); 831 break; 832 } 833 if (ok) 834 TIFFSetField(tif, dir->tdir_tag, v[0], v[1]); 835 return (ok); 836} 837 838/* 839 * Fetch an array of LONG or SLONG values. 840 */ 841static int 842TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v) 843{ 844 if (dir->tdir_count == 1) { 845 v[0] = dir->tdir_offset; 846 return (1); 847 } else 848 return (TIFFFetchData(tif, dir, (char*) v) != 0); 849} 850 851/* 852 * Fetch an array of RATIONAL or SRATIONAL values. 853 */ 854static int 855TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v) 856{ 857 int ok = 0; 858 uint32* l; 859 860 l = (uint32*)CheckMalloc(tif, 861 dir->tdir_count*tiffDataWidth[dir->tdir_type], 862 "to fetch array of rationals"); 863 if (l) { 864 if (TIFFFetchData(tif, dir, (char *)l)) { 865 uint32 i; 866 for (i = 0; i < dir->tdir_count; i++) { 867 ok = cvtRational(tif, dir, 868 l[2*i+0], l[2*i+1], &v[i]); 869 if (!ok) 870 break; 871 } 872 } 873 _TIFFfree(tif, (char *)l); 874 } 875 return (ok); 876} 877 878/* 879 * Fetch an array of FLOAT values. 880 */ 881static int 882TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v) 883{ 884 885 if (dir->tdir_count == 1) { 886 v[0] = *(float*) &dir->tdir_offset; 887 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v); 888 return (1); 889 } else if (TIFFFetchData(tif, dir, (char*) v)) { 890 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v); 891 return (1); 892 } else 893 return (0); 894} 895 896/* 897 * Fetch an array of DOUBLE values. 898 */ 899static int 900TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v) 901{ 902 if (TIFFFetchData(tif, dir, (char*) v)) { 903 TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v); 904 return (1); 905 } else 906 return (0); 907} 908 909/* 910 * Fetch an array of ANY values. The actual values are 911 * returned as doubles which should be able hold all the 912 * types. Yes, there really should be an tany_t to avoid 913 * this potential non-portability ... Note in particular 914 * that we assume that the double return value vector is 915 * large enough to read in any fundamental type. We use 916 * that vector as a buffer to read in the base type vector 917 * and then convert it in place to double (from end 918 * to front of course). 919 */ 920static int 921TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v) 922{ 923 int i; 924 925 switch (dir->tdir_type) { 926 case TIFF_BYTE: 927 case TIFF_SBYTE: 928 if (!TIFFFetchByteArray(tif, dir, (uint16*) v)) 929 return (0); 930 if (dir->tdir_type == TIFF_BYTE) { 931 uint16* vp = (uint16*) v; 932 for (i = dir->tdir_count-1; i >= 0; i--) 933 v[i] = vp[i]; 934 } else { 935 int16* vp = (int16*) v; 936 for (i = dir->tdir_count-1; i >= 0; i--) 937 v[i] = vp[i]; 938 } 939 break; 940 case TIFF_SHORT: 941 case TIFF_SSHORT: 942 if (!TIFFFetchShortArray(tif, dir, (uint16*) v)) 943 return (0); 944 if (dir->tdir_type == TIFF_SHORT) { 945 uint16* vp = (uint16*) v; 946 for (i = dir->tdir_count-1; i >= 0; i--) 947 v[i] = vp[i]; 948 } else { 949 int16* vp = (int16*) v; 950 for (i = dir->tdir_count-1; i >= 0; i--) 951 v[i] = vp[i]; 952 } 953 break; 954 case TIFF_LONG: 955 case TIFF_SLONG: 956 if (!TIFFFetchLongArray(tif, dir, (uint32*) v)) 957 return (0); 958 if (dir->tdir_type == TIFF_LONG) { 959 uint32* vp = (uint32*) v; 960 for (i = dir->tdir_count-1; i >= 0; i--) 961 v[i] = vp[i]; 962 } else { 963 int32* vp = (int32*) v; 964 for (i = dir->tdir_count-1; i >= 0; i--) 965 v[i] = vp[i]; 966 } 967 break; 968 case TIFF_RATIONAL: 969 case TIFF_SRATIONAL: 970 if (!TIFFFetchRationalArray(tif, dir, (float*) v)) 971 return (0); 972 { float* vp = (float*) v; 973 for (i = dir->tdir_count-1; i >= 0; i--) 974 v[i] = vp[i]; 975 } 976 break; 977 case TIFF_FLOAT: 978 if (!TIFFFetchFloatArray(tif, dir, (float*) v)) 979 return (0); 980 { float* vp = (float*) v; 981 for (i = dir->tdir_count-1; i >= 0; i--) 982 v[i] = vp[i]; 983 } 984 break; 985 case TIFF_DOUBLE: 986 return (TIFFFetchDoubleArray(tif, dir, (double*) v)); 987 default: 988 /* TIFF_NOTYPE */ 989 /* TIFF_ASCII */ 990 /* TIFF_UNDEFINED */ 991 TIFFError(tif->tif_name, 992 "Cannot read TIFF_ANY type %d for field \"%s\"", 993 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); 994 return (0); 995 } 996 return (1); 997} 998 999/* 1000 * Fetch a tag that is not handled by special case code. 1001 */ 1002static int 1003TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp) 1004{ 1005 static const char mesg[] = "to fetch tag value"; 1006 int ok = 0; 1007 const TIFFFieldInfo* fip = _TIFFFieldWithTag(tif, dp->tdir_tag); 1008 1009 if (dp->tdir_count > 1) { /* array of values */ 1010 char* cp = NULL; 1011 1012 switch (dp->tdir_type) { 1013 case TIFF_BYTE: 1014 case TIFF_SBYTE: 1015 /* NB: always expand BYTE values to shorts */ 1016 cp = CheckMalloc(tif, 1017 dp->tdir_count * sizeof (uint16), mesg); 1018 ok = cp && TIFFFetchByteArray(tif, dp, (uint16*) cp); 1019 break; 1020 case TIFF_SHORT: 1021 case TIFF_SSHORT: 1022 cp = CheckMalloc(tif, 1023 dp->tdir_count * sizeof (uint16), mesg); 1024 ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp); 1025 break; 1026 case TIFF_LONG: 1027 case TIFF_SLONG: 1028 cp = CheckMalloc(tif, 1029 dp->tdir_count * sizeof (uint32), mesg); 1030 ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp); 1031 break; 1032 case TIFF_RATIONAL: 1033 case TIFF_SRATIONAL: 1034 cp = CheckMalloc(tif, 1035 dp->tdir_count * sizeof (float), mesg); 1036 ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp); 1037 break; 1038 case TIFF_FLOAT: 1039 cp = CheckMalloc(tif, 1040 dp->tdir_count * sizeof (float), mesg); 1041 ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp); 1042 break; 1043 case TIFF_DOUBLE: 1044 cp = CheckMalloc(tif, 1045 dp->tdir_count * sizeof (double), mesg); 1046 ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp); 1047 break; 1048 case TIFF_ASCII: 1049 case TIFF_UNDEFINED: /* bit of a cheat... */ 1050 /* 1051 * Some vendors write strings w/o the trailing 1052 * NULL byte, so always append one just in case. 1053 */ 1054 cp = CheckMalloc(tif, dp->tdir_count+1, mesg); 1055 if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 ) 1056 cp[dp->tdir_count] = '\0'; /* XXX */ 1057 break; 1058 } 1059 if (ok) { 1060 ok = (fip->field_passcount ? 1061 TIFFSetField(tif, dp->tdir_tag, dp->tdir_count, cp) 1062 : TIFFSetField(tif, dp->tdir_tag, cp)); 1063 } 1064 if (cp != NULL) 1065 _TIFFfree(tif, cp); 1066 } else if (CheckDirCount(tif, dp, 1)) { /* singleton value */ 1067 switch (dp->tdir_type) { 1068 case TIFF_BYTE: 1069 case TIFF_SBYTE: 1070 case TIFF_SHORT: 1071 case TIFF_SSHORT: 1072 /* 1073 * If the tag is also acceptable as a LONG or SLONG 1074 * then TIFFSetField will expect an uint32 parameter 1075 * passed to it (through varargs). Thus, for machines 1076 * where sizeof (int) != sizeof (uint32) we must do 1077 * a careful check here. It's hard to say if this 1078 * is worth optimizing. 1079 * 1080 * NB: We use TIFFFieldWithTag here knowing that 1081 * it returns us the first entry in the table 1082 * for the tag and that that entry is for the 1083 * widest potential data type the tag may have. 1084 */ 1085 { TIFFDataType type = fip->field_type; 1086 if (type != TIFF_LONG && type != TIFF_SLONG) { 1087 uint16 v = (uint16) 1088 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset); 1089 ok = (fip->field_passcount ? 1090 TIFFSetField(tif, dp->tdir_tag, 1, &v) 1091 : TIFFSetField(tif, dp->tdir_tag, v)); 1092 break; 1093 } 1094 } 1095 /* fall thru... */ 1096 case TIFF_LONG: 1097 case TIFF_SLONG: 1098 { uint32 v32 = 1099 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset); 1100 ok = (fip->field_passcount ? 1101 TIFFSetField(tif, dp->tdir_tag, 1, &v32) 1102 : TIFFSetField(tif, dp->tdir_tag, v32)); 1103 } 1104 break; 1105 case TIFF_RATIONAL: 1106 case TIFF_SRATIONAL: 1107 case TIFF_FLOAT: 1108 { float v = (dp->tdir_type == TIFF_FLOAT ? 1109 TIFFFetchFloat(tif, dp) 1110 : TIFFFetchRational(tif, dp)); 1111 ok = (fip->field_passcount ? 1112 TIFFSetField(tif, dp->tdir_tag, 1, &v) 1113 : TIFFSetField(tif, dp->tdir_tag, v)); 1114 } 1115 break; 1116 case TIFF_DOUBLE: 1117 { double v; 1118 ok = (TIFFFetchDoubleArray(tif, dp, &v) && 1119 (fip->field_passcount ? 1120 TIFFSetField(tif, dp->tdir_tag, 1, &v) 1121 : TIFFSetField(tif, dp->tdir_tag, v)) 1122 ); 1123 } 1124 break; 1125 case TIFF_ASCII: 1126 case TIFF_UNDEFINED: /* bit of a cheat... */ 1127 { char c[2]; 1128 if( (ok = (TIFFFetchString(tif, dp, c) != 0)) != 0 ){ 1129 c[1] = '\0'; /* XXX paranoid */ 1130 ok = TIFFSetField(tif, dp->tdir_tag, c); 1131 } 1132 } 1133 break; 1134 } 1135 } 1136 return (ok); 1137} 1138 1139#define NITEMS(x) (sizeof (x) / sizeof (x[0])) 1140/* 1141 * Fetch samples/pixel short values for 1142 * the specified tag and verify that 1143 * all values are the same. 1144 */ 1145static int 1146TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, int* pl) 1147{ 1148 int samples = tif->tif_dir.td_samplesperpixel; 1149 int status = 0; 1150 1151 if (CheckDirCount(tif, dir, (uint32) samples)) { 1152 uint16 buf[10]; 1153 uint16* v = buf; 1154 1155 if (samples > (int)NITEMS(buf)) 1156 v=(uint16*) _TIFFmalloc(tif, samples * sizeof (uint16)); 1157 if (TIFFFetchShortArray(tif, dir, v)) { 1158 int i; 1159 for (i = 1; i < samples; i++) 1160 if (v[i] != v[0]) { 1161 TIFFError(tif->tif_name, 1162 "Cannot handle different per-sample values for field \"%s\"", 1163 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); 1164 goto bad; 1165 } 1166 *pl = v[0]; 1167 status = 1; 1168 } 1169 bad: 1170 if (v != buf) 1171 _TIFFfree(tif, (char*) v); 1172 } 1173 return (status); 1174} 1175 1176/* 1177 * Fetch samples/pixel ANY values for 1178 * the specified tag and verify that 1179 * all values are the same. 1180 */ 1181static int 1182TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl) 1183{ 1184 int samples = (int) tif->tif_dir.td_samplesperpixel; 1185 int status = 0; 1186 1187 if (CheckDirCount(tif, dir, (uint32) samples)) { 1188 double buf[10]; 1189 double* v = buf; 1190 1191 if (samples > (int)NITEMS(buf)) 1192 v=(double*) _TIFFmalloc(tif, samples * sizeof (double)); 1193 if (TIFFFetchAnyArray(tif, dir, v)) { 1194 int i; 1195 for (i = 1; i < samples; i++) 1196 if (v[i] != v[0]) { 1197 TIFFError(tif->tif_name, 1198 "Cannot handle different per-sample values for field \"%s\"", 1199 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); 1200 goto bad; 1201 } 1202 *pl = v[0]; 1203 status = 1; 1204 } 1205 bad: 1206 if (v != buf) 1207 _TIFFfree(tif, v); 1208 } 1209 return (status); 1210} 1211#undef NITEMS 1212 1213/* 1214 * Fetch a set of offsets or lengths. 1215 * While this routine says "strips", 1216 * in fact it's also used for tiles. 1217 */ 1218static int 1219TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp) 1220{ 1221 register uint32* lp; 1222 int status; 1223 1224 if (!CheckDirCount(tif, dir, (uint32) nstrips)) 1225 return (0); 1226 /* 1227 * Allocate space for strip information. 1228 */ 1229 if (*lpp == NULL && 1230 (*lpp = (uint32 *)CheckMalloc(tif, 1231 nstrips * sizeof (uint32), "for strip array")) == NULL) 1232 return (0); 1233 lp = *lpp; 1234 if (dir->tdir_type == (int)TIFF_SHORT) { 1235 /* 1236 * Handle uint16->uint32 expansion. 1237 */ 1238 uint16* dp = (uint16*) CheckMalloc(tif, 1239 dir->tdir_count* sizeof (uint16), "to fetch strip tag"); 1240 if (dp == NULL) 1241 return (0); 1242 if( (status = TIFFFetchShortArray(tif, dir, dp)) != 0 ) { 1243 register uint16* wp = dp; 1244 while (nstrips-- > 0) 1245 *lp++ = *wp++; 1246 } 1247 _TIFFfree(tif, (char*) dp); 1248 } else 1249 status = TIFFFetchLongArray(tif, dir, lp); 1250 return (status); 1251} 1252 1253#define NITEMS(x) (sizeof (x) / sizeof (x[0])) 1254/* 1255 * Fetch and set the ExtraSamples tag. 1256 */ 1257static int 1258TIFFFetchExtraSamples(TIFF* tif, TIFFDirEntry* dir) 1259{ 1260 uint16 buf[10]; 1261 uint16* v = buf; 1262 int status; 1263 1264 if (dir->tdir_count > NITEMS(buf)) 1265 v=(uint16*) _TIFFmalloc(tif, dir->tdir_count * sizeof (uint16)); 1266 if (dir->tdir_type == TIFF_BYTE) 1267 status = TIFFFetchByteArray(tif, dir, v); 1268 else 1269 status = TIFFFetchShortArray(tif, dir, v); 1270 if (status) 1271 status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v); 1272 if (v != buf) 1273 _TIFFfree(tif, (char*) v); 1274 return (status); 1275} 1276#undef NITEMS 1277 1278#ifdef COLORIMETRY_SUPPORT 1279/* 1280 * Fetch and set the RefBlackWhite tag. 1281 */ 1282static int 1283TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir) 1284{ 1285 static const char mesg[] = "for \"ReferenceBlackWhite\" array"; 1286 char* cp; 1287 int ok; 1288 1289 if (dir->tdir_type == TIFF_RATIONAL) 1290 return (TIFFFetchNormalTag(tif, dir)); 1291 /* 1292 * Handle LONG's for backward compatibility. 1293 */ 1294 cp = CheckMalloc(tif, dir->tdir_count * sizeof (uint32), mesg); 1295 if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) { 1296 float* fp = (float*) 1297 CheckMalloc(tif, dir->tdir_count * sizeof (float), mesg); 1298 if( (ok = (fp != NULL)) != 0 ) { 1299 uint32 i; 1300 for (i = 0; i < dir->tdir_count; i++) 1301 fp[i] = (float)((uint32*) cp)[i]; 1302 ok = TIFFSetField(tif, dir->tdir_tag, fp); 1303 _TIFFfree(tif, (char*) fp); 1304 } 1305 } 1306 if (cp) 1307 _TIFFfree(tif, cp); 1308 return (ok); 1309} 1310#endif 1311 1312/* 1313 * Replace a single strip (tile) of uncompressed data by 1314 * multiple strips (tiles), each approximately 8Kbytes. 1315 * This is useful for dealing with large images or 1316 * for dealing with machines with a limited amount 1317 * memory. 1318 */ 1319static void 1320ChopUpSingleUncompressedStrip(TIFF* tif) 1321{ 1322 register TIFFDirectory *td = &tif->tif_dir; 1323 uint32 bytecount = td->td_stripbytecount[0]; 1324 uint32 offset = td->td_stripoffset[0]; 1325 tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes; 1326 tstrip_t strip, nstrips, rowsperstrip; 1327 uint32* newcounts; 1328 uint32* newoffsets; 1329 1330 /* 1331 * Make the rows hold at least one 1332 * scanline, but fill 8k if possible. 1333 */ 1334 if (rowbytes > 8192) { 1335 stripbytes = rowbytes; 1336 rowsperstrip = 1; 1337 } else { 1338 rowsperstrip = 8192 / rowbytes; 1339 stripbytes = rowbytes * rowsperstrip; 1340 } 1341 /* never increase the number of strips in an image */ 1342 if (rowsperstrip >= td->td_rowsperstrip) 1343 return; 1344 nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes); 1345 newcounts = (uint32*) CheckMalloc(tif, nstrips * sizeof (uint32), 1346 "for chopped \"StripByteCounts\" array"); 1347 newoffsets = (uint32*) CheckMalloc(tif, nstrips * sizeof (uint32), 1348 "for chopped \"StripOffsets\" array"); 1349 if (newcounts == NULL || newoffsets == NULL) { 1350 /* 1351 * Unable to allocate new strip information, give 1352 * up and use the original one strip information. 1353 */ 1354 if (newcounts != NULL) 1355 _TIFFfree(tif, newcounts); 1356 if (newoffsets != NULL) 1357 _TIFFfree(tif, newoffsets); 1358 return; 1359 } 1360 /* 1361 * Fill the strip information arrays with 1362 * new bytecounts and offsets that reflect 1363 * the broken-up format. 1364 */ 1365 for (strip = 0; strip < nstrips; strip++) { 1366 if (stripbytes > (tsize_t) bytecount) 1367 stripbytes = bytecount; 1368 newcounts[strip] = stripbytes; 1369 newoffsets[strip] = offset; 1370 offset += stripbytes; 1371 bytecount -= stripbytes; 1372 } 1373 /* 1374 * Replace old single strip info with multi-strip info. 1375 */ 1376 td->td_stripsperimage = td->td_nstrips = nstrips; 1377 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip); 1378 1379 _TIFFfree(tif, td->td_stripbytecount); 1380 _TIFFfree(tif, td->td_stripoffset); 1381 td->td_stripbytecount = newcounts; 1382 td->td_stripoffset = newoffsets; 1383} 1384