4  * Copyright (c) 1988-1997 Sam Leffler 
   5  * Copyright (c) 1991-1997 Silicon Graphics, Inc. 
   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. 
  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.   
  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  
  30  * Directory Read Support Routines. 
  34 #define IGNORE  0               /* tag placeholder used below */ 
  37 # define        TIFFCvtIEEEFloatToNative(tif, n, fp) 
  38 # define        TIFFCvtIEEEDoubleToNative(tif, n, dp) 
  40 extern  void TIFFCvtIEEEFloatToNative(TIFF
*, uint32
, float*); 
  41 extern  void TIFFCvtIEEEDoubleToNative(TIFF
*, uint32
, double*); 
  44 static  int EstimateStripByteCounts(TIFF
*, TIFFDirEntry
*, uint16
); 
  45 static  void MissingRequired(TIFF
*, const char*); 
  46 static  int CheckDirCount(TIFF
*, TIFFDirEntry
*, uint32
); 
  47 static  tsize_t 
TIFFFetchData(TIFF
*, TIFFDirEntry
*, char*); 
  48 static  tsize_t 
TIFFFetchString(TIFF
*, TIFFDirEntry
*, char*); 
  49 static  float TIFFFetchRational(TIFF
*, TIFFDirEntry
*); 
  50 static  int TIFFFetchNormalTag(TIFF
*, TIFFDirEntry
*); 
  51 static  int TIFFFetchPerSampleShorts(TIFF
*, TIFFDirEntry
*, uint16
*); 
  52 static  int TIFFFetchPerSampleLongs(TIFF
*, TIFFDirEntry
*, uint32
*); 
  53 static  int TIFFFetchPerSampleAnys(TIFF
*, TIFFDirEntry
*, double*); 
  54 static  int TIFFFetchShortArray(TIFF
*, TIFFDirEntry
*, uint16
*); 
  55 static  int TIFFFetchStripThing(TIFF
*, TIFFDirEntry
*, long, uint32
**); 
  56 static  int TIFFFetchRefBlackWhite(TIFF
*, TIFFDirEntry
*); 
  57 static  float TIFFFetchFloat(TIFF
*, TIFFDirEntry
*); 
  58 static  int TIFFFetchFloatArray(TIFF
*, TIFFDirEntry
*, float*); 
  59 static  int TIFFFetchDoubleArray(TIFF
*, TIFFDirEntry
*, double*); 
  60 static  int TIFFFetchAnyArray(TIFF
*, TIFFDirEntry
*, double*); 
  61 static  int TIFFFetchShortPair(TIFF
*, TIFFDirEntry
*); 
  62 static  void ChopUpSingleUncompressedStrip(TIFF
*); 
  65  * Read the next TIFF directory from a file 
  66  * and convert it to the internal format. 
  67  * We read directories sequentially. 
  70 TIFFReadDirectory(TIFF
* tif
) 
  72         static const char module[] = "TIFFReadDirectory"; 
  76         TIFFDirEntry 
*dp
, *dir 
= NULL
; 
  79         const TIFFFieldInfo
* fip
; 
  83         int diroutoforderwarning 
= 0; 
  86         tif
->tif_diroff 
= tif
->tif_nextdiroff
; 
  87         if (tif
->tif_diroff 
== 0)               /* no more directories */ 
  91          * XXX: Trick to prevent IFD looping. The one can create TIFF file 
  92          * with looped directory pointers. We will maintain a list of already 
  93          * seen directories and check every IFD offset against this list. 
  95         for (n 
= 0; n 
< tif
->tif_dirnumber
; n
++) { 
  96                 if (tif
->tif_dirlist
[n
] == tif
->tif_diroff
) 
 100         new_dirlist 
= (toff_t 
*)_TIFFrealloc(tif
->tif_dirlist
, 
 101                                         tif
->tif_dirnumber 
* sizeof(toff_t
)); 
 103                 TIFFErrorExt(tif
->tif_clientdata
, module, 
 104                           "%s: Failed to allocate space for IFD list", 
 108         tif
->tif_dirlist 
= new_dirlist
; 
 109         tif
->tif_dirlist
[tif
->tif_dirnumber 
- 1] = tif
->tif_diroff
; 
 112          * Cleanup any previous compression state. 
 114         (*tif
->tif_cleanup
)(tif
); 
 117         if (!isMapped(tif
)) { 
 118                 if (!SeekOK(tif
, tif
->tif_diroff
)) { 
 119                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 120                             "%s: Seek error accessing TIFF directory", 
 124                 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) { 
 125                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 126                             "%s: Can not read TIFF directory count", 
 130                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 131                         TIFFSwabShort(&dircount
); 
 132                 dir 
= (TIFFDirEntry 
*)_TIFFCheckMalloc(tif
, dircount
, 
 133                                                        sizeof (TIFFDirEntry
), 
 134                                                 "to read TIFF directory"); 
 137                 if (!ReadOK(tif
, dir
, dircount
*sizeof (TIFFDirEntry
))) { 
 138                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 139                                   "%.100s: Can not read TIFF directory", 
 144                  * Read offset to next directory for sequential scans. 
 146                 (void) ReadOK(tif
, &nextdiroff
, sizeof (uint32
)); 
 148                 toff_t off 
= tif
->tif_diroff
; 
 150                 if (off 
+ sizeof (uint16
) > tif
->tif_size
) { 
 151                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 152                             "%s: Can not read TIFF directory count", 
 156                         _TIFFmemcpy(&dircount
, tif
->tif_base 
+ off
, sizeof (uint16
)); 
 157                 off 
+= sizeof (uint16
); 
 158                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 159                         TIFFSwabShort(&dircount
); 
 160                 dir 
= (TIFFDirEntry 
*)_TIFFCheckMalloc(tif
, dircount
, 
 161                                                        sizeof (TIFFDirEntry
), 
 162                                                 "to read TIFF directory"); 
 165                 if (off 
+ dircount
*sizeof (TIFFDirEntry
) > tif
->tif_size
) { 
 166                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 167                                   "%s: Can not read TIFF directory", 
 171                         _TIFFmemcpy(dir
, tif
->tif_base 
+ off
, 
 172                                     dircount
*sizeof (TIFFDirEntry
)); 
 174                 off 
+= dircount
* sizeof (TIFFDirEntry
); 
 175                 if (off 
+ sizeof (uint32
) <= tif
->tif_size
) 
 176                         _TIFFmemcpy(&nextdiroff
, tif
->tif_base
+off
, sizeof (uint32
)); 
 178         if (tif
->tif_flags 
& TIFF_SWAB
) 
 179                 TIFFSwabLong(&nextdiroff
); 
 180         tif
->tif_nextdiroff 
= nextdiroff
; 
 182         tif
->tif_flags 
&= ~TIFF_BEENWRITING
;    /* reset before new dir */ 
 184          * Setup default value and then make a pass over 
 185          * the fields to check type and tag information, 
 186          * and to extract info required to size data 
 187          * structures.  A second pass is made afterwards 
 188          * to read in everthing not taken in the first pass. 
 191         /* free any old stuff and reinit */ 
 192         TIFFFreeDirectory(tif
); 
 193         TIFFDefaultDirectory(tif
); 
 195          * Electronic Arts writes gray-scale TIFF files 
 196          * without a PlanarConfiguration directory entry. 
 197          * Thus we setup a default value here, even though 
 198          * the TIFF spec says there is no default value. 
 200         TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
); 
 203          * Sigh, we must make a separate pass through the 
 204          * directory for the following reason: 
 206          * We must process the Compression tag in the first pass 
 207          * in order to merge in codec-private tag definitions (otherwise 
 208          * we may get complaints about unknown tags).  However, the 
 209          * Compression tag may be dependent on the SamplesPerPixel 
 210          * tag value because older TIFF specs permited Compression 
 211          * to be written as a SamplesPerPixel-count tag entry. 
 212          * Thus if we don't first figure out the correct SamplesPerPixel 
 213          * tag value then we may end up ignoring the Compression tag 
 214          * value because it has an incorrect count value (if the 
 215          * true value of SamplesPerPixel is not 1). 
 217          * It sure would have been nice if Aldus had really thought 
 218          * this stuff through carefully. 
 220         for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 221                 if (tif
->tif_flags 
& TIFF_SWAB
) { 
 222                         TIFFSwabArrayOfShort(&dp
->tdir_tag
, 2); 
 223                         TIFFSwabArrayOfLong(&dp
->tdir_count
, 2); 
 225                 if (dp
->tdir_tag 
== TIFFTAG_SAMPLESPERPIXEL
) { 
 226                         if (!TIFFFetchNormalTag(tif
, dp
)) 
 228                         dp
->tdir_tag 
= IGNORE
; 
 232          * First real pass over the directory. 
 235         for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 237                 if (fix 
>= tif
->tif_nfields 
|| dp
->tdir_tag 
== IGNORE
) 
 241                  * Silicon Beach (at least) writes unordered 
 242                  * directory tags (violating the spec).  Handle 
 243                  * it here, but be obnoxious (maybe they'll fix it?). 
 245                 if (dp
->tdir_tag 
< tif
->tif_fieldinfo
[fix
]->field_tag
) { 
 246                         if (!diroutoforderwarning
) { 
 247                                 TIFFWarningExt(tif
->tif_clientdata
, module, 
 248         "%s: invalid TIFF directory; tags are not sorted in ascending order", 
 250                                 diroutoforderwarning 
= 1; 
 252                         fix 
= 0;                        /* O(n^2) */ 
 254                 while (fix 
< tif
->tif_nfields 
&& 
 255                        tif
->tif_fieldinfo
[fix
]->field_tag 
< dp
->tdir_tag
) 
 257                 if (fix 
>= tif
->tif_nfields 
|| 
 258                     tif
->tif_fieldinfo
[fix
]->field_tag 
!= dp
->tdir_tag
) { 
 260                                         TIFFWarningExt(tif
->tif_clientdata
, 
 262                         "%s: unknown field with tag %d (0x%x) encountered", 
 268                     TIFFMergeFieldInfo(tif
, 
 269                                        _TIFFCreateAnonFieldInfo(tif
, 
 271                                                 (TIFFDataType
) dp
->tdir_type
), 
 274                     while (fix 
< tif
->tif_nfields 
&& 
 275                            tif
->tif_fieldinfo
[fix
]->field_tag 
< dp
->tdir_tag
) 
 279                  * Null out old tags that we ignore. 
 281                 if (tif
->tif_fieldinfo
[fix
]->field_bit 
== FIELD_IGNORE
) { 
 283                         dp
->tdir_tag 
= IGNORE
; 
 289                 fip 
= tif
->tif_fieldinfo
[fix
]; 
 290                 while (dp
->tdir_type 
!= (unsigned short) fip
->field_type
 
 291                        && fix 
< tif
->tif_nfields
) { 
 292                         if (fip
->field_type 
== TIFF_ANY
)        /* wildcard */ 
 294                         fip 
= tif
->tif_fieldinfo
[++fix
]; 
 295                         if (fix 
>= tif
->tif_nfields 
|| 
 296                             fip
->field_tag 
!= dp
->tdir_tag
) { 
 297                                 TIFFWarningExt(tif
->tif_clientdata
, module, 
 298                         "%s: wrong data type %d for \"%s\"; tag ignored", 
 299                                             tif
->tif_name
, dp
->tdir_type
, 
 300                                             tif
->tif_fieldinfo
[fix
-1]->field_name
); 
 305                  * Check count if known in advance. 
 307                 if (fip
->field_readcount 
!= TIFF_VARIABLE
 
 308                     && fip
->field_readcount 
!= TIFF_VARIABLE2
) { 
 309                         uint32 expected 
= (fip
->field_readcount 
== TIFF_SPP
) ? 
 310                             (uint32
) td
->td_samplesperpixel 
: 
 311                             (uint32
) fip
->field_readcount
; 
 312                         if (!CheckDirCount(tif
, dp
, expected
)) 
 316                 switch (dp
->tdir_tag
) { 
 317                 case TIFFTAG_COMPRESSION
: 
 319                          * The 5.0 spec says the Compression tag has 
 320                          * one value, while earlier specs say it has 
 321                          * one value per sample.  Because of this, we 
 322                          * accept the tag if one value is supplied. 
 324                         if (dp
->tdir_count 
== 1) { 
 325                                 v 
= TIFFExtractData(tif
, 
 326                                     dp
->tdir_type
, dp
->tdir_offset
); 
 327                                 if (!TIFFSetField(tif
, dp
->tdir_tag
, (uint16
)v
)) 
 330                         /* XXX: workaround for broken TIFFs */ 
 331                         } else if (dp
->tdir_type 
== TIFF_LONG
) { 
 332                                 if (!TIFFFetchPerSampleLongs(tif
, dp
, &v
) || 
 333                                     !TIFFSetField(tif
, dp
->tdir_tag
, (uint16
)v
)) 
 336                                 if (!TIFFFetchPerSampleShorts(tif
, dp
, &iv
) 
 337                                     || !TIFFSetField(tif
, dp
->tdir_tag
, iv
)) 
 340                         dp
->tdir_tag 
= IGNORE
; 
 342                 case TIFFTAG_STRIPOFFSETS
: 
 343                 case TIFFTAG_STRIPBYTECOUNTS
: 
 344                 case TIFFTAG_TILEOFFSETS
: 
 345                 case TIFFTAG_TILEBYTECOUNTS
: 
 346                         TIFFSetFieldBit(tif
, fip
->field_bit
); 
 348                 case TIFFTAG_IMAGEWIDTH
: 
 349                 case TIFFTAG_IMAGELENGTH
: 
 350                 case TIFFTAG_IMAGEDEPTH
: 
 351                 case TIFFTAG_TILELENGTH
: 
 352                 case TIFFTAG_TILEWIDTH
: 
 353                 case TIFFTAG_TILEDEPTH
: 
 354                 case TIFFTAG_PLANARCONFIG
: 
 355                 case TIFFTAG_ROWSPERSTRIP
: 
 356                 case TIFFTAG_EXTRASAMPLES
: 
 357                         if (!TIFFFetchNormalTag(tif
, dp
)) 
 359                         dp
->tdir_tag 
= IGNORE
; 
 365          * Allocate directory structure and setup defaults. 
 367         if (!TIFFFieldSet(tif
, FIELD_IMAGEDIMENSIONS
)) { 
 368                 MissingRequired(tif
, "ImageLength"); 
 372          * Setup appropriate structures (by strip or by tile) 
 374         if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) { 
 375                 td
->td_nstrips 
= TIFFNumberOfStrips(tif
); 
 376                 td
->td_tilewidth 
= td
->td_imagewidth
; 
 377                 td
->td_tilelength 
= td
->td_rowsperstrip
; 
 378                 td
->td_tiledepth 
= td
->td_imagedepth
; 
 379                 tif
->tif_flags 
&= ~TIFF_ISTILED
; 
 381                 td
->td_nstrips 
= TIFFNumberOfTiles(tif
); 
 382                 tif
->tif_flags 
|= TIFF_ISTILED
; 
 384         if (!td
->td_nstrips
) { 
 385                 TIFFErrorExt(tif
->tif_clientdata
, module, 
 386                              "%s: cannot handle zero number of %s", 
 387                              tif
->tif_name
, isTiled(tif
) ? "tiles" : "strips"); 
 390         td
->td_stripsperimage 
= td
->td_nstrips
; 
 391         if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE
) 
 392                 td
->td_stripsperimage 
/= td
->td_samplesperpixel
; 
 393         if (!TIFFFieldSet(tif
, FIELD_STRIPOFFSETS
)) { 
 395                                 isTiled(tif
) ? "TileOffsets" : "StripOffsets"); 
 400          * Second pass: extract other information. 
 402         for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 403                 if (dp
->tdir_tag 
== IGNORE
) 
 405                 switch (dp
->tdir_tag
) { 
 406                 case TIFFTAG_MINSAMPLEVALUE
: 
 407                 case TIFFTAG_MAXSAMPLEVALUE
: 
 408                 case TIFFTAG_BITSPERSAMPLE
: 
 409                 case TIFFTAG_DATATYPE
: 
 410                 case TIFFTAG_SAMPLEFORMAT
: 
 412                          * The 5.0 spec says the Compression tag has 
 413                          * one value, while earlier specs say it has 
 414                          * one value per sample.  Because of this, we 
 415                          * accept the tag if one value is supplied. 
 417                          * The MinSampleValue, MaxSampleValue, BitsPerSample 
 418                          * DataType and SampleFormat tags are supposed to be 
 419                          * written as one value/sample, but some vendors 
 420                          * incorrectly write one value only -- so we accept 
 421                          * that as well (yech). Other vendors write correct 
 422                          * value for NumberOfSamples, but incorrect one for 
 423                          * BitsPerSample and friends, and we will read this 
 426                         if (dp
->tdir_count 
== 1) { 
 427                                 v 
= TIFFExtractData(tif
, 
 428                                     dp
->tdir_type
, dp
->tdir_offset
); 
 429                                 if (!TIFFSetField(tif
, dp
->tdir_tag
, (uint16
)v
)) 
 431                         /* XXX: workaround for broken TIFFs */ 
 432                         } else if (dp
->tdir_tag 
== TIFFTAG_BITSPERSAMPLE
 
 433                                    && dp
->tdir_type 
== TIFF_LONG
) { 
 434                                 if (!TIFFFetchPerSampleLongs(tif
, dp
, &v
) || 
 435                                     !TIFFSetField(tif
, dp
->tdir_tag
, (uint16
)v
)) 
 438                                 if (!TIFFFetchPerSampleShorts(tif
, dp
, &iv
) || 
 439                                     !TIFFSetField(tif
, dp
->tdir_tag
, iv
)) 
 443                 case TIFFTAG_SMINSAMPLEVALUE
: 
 444                 case TIFFTAG_SMAXSAMPLEVALUE
: 
 447                                 if (!TIFFFetchPerSampleAnys(tif
, dp
, &dv
) || 
 448                                     !TIFFSetField(tif
, dp
->tdir_tag
, dv
)) 
 452                 case TIFFTAG_STRIPOFFSETS
: 
 453                 case TIFFTAG_TILEOFFSETS
: 
 454                         if (!TIFFFetchStripThing(tif
, dp
, 
 455                             td
->td_nstrips
, &td
->td_stripoffset
)) 
 458                 case TIFFTAG_STRIPBYTECOUNTS
: 
 459                 case TIFFTAG_TILEBYTECOUNTS
: 
 460                         if (!TIFFFetchStripThing(tif
, dp
, 
 461                             td
->td_nstrips
, &td
->td_stripbytecount
)) 
 464                 case TIFFTAG_COLORMAP
: 
 465                 case TIFFTAG_TRANSFERFUNCTION
: 
 469                                  * TransferFunction can have either 1x or 3x 
 470                                  * data values; Colormap can have only 3x 
 473                                 v 
= 1L<<td
->td_bitspersample
; 
 474                                 if (dp
->tdir_tag 
== TIFFTAG_COLORMAP 
|| 
 475                                     dp
->tdir_count 
!= v
) { 
 476                                         if (!CheckDirCount(tif
, dp
, 3 * v
)) 
 480                                 cp 
= (char *)_TIFFCheckMalloc(tif
, 
 483                                         "to read \"TransferFunction\" tag"); 
 485                                         if (TIFFFetchData(tif
, dp
, cp
)) { 
 487                                                  * This deals with there being 
 488                                                  * only one array to apply to 
 491                                                 uint32 c 
= 1L << td
->td_bitspersample
; 
 492                                                 if (dp
->tdir_count 
== c
) 
 494                                                 TIFFSetField(tif
, dp
->tdir_tag
, 
 501                 case TIFFTAG_PAGENUMBER
: 
 502                 case TIFFTAG_HALFTONEHINTS
: 
 503                 case TIFFTAG_YCBCRSUBSAMPLING
: 
 504                 case TIFFTAG_DOTRANGE
: 
 505                         (void) TIFFFetchShortPair(tif
, dp
); 
 507                 case TIFFTAG_REFERENCEBLACKWHITE
: 
 508                         (void) TIFFFetchRefBlackWhite(tif
, dp
); 
 510 /* BEGIN REV 4.0 COMPATIBILITY */ 
 511                 case TIFFTAG_OSUBFILETYPE
: 
 513                         switch (TIFFExtractData(tif
, dp
->tdir_type
, 
 515                         case OFILETYPE_REDUCEDIMAGE
: 
 516                                 v 
= FILETYPE_REDUCEDIMAGE
; 
 523                                 TIFFSetField(tif
, TIFFTAG_SUBFILETYPE
, v
); 
 525 /* END REV 4.0 COMPATIBILITY */ 
 527                         (void) TIFFFetchNormalTag(tif
, dp
); 
 532          * Verify Palette image has a Colormap. 
 534         if (td
->td_photometric 
== PHOTOMETRIC_PALETTE 
&& 
 535             !TIFFFieldSet(tif
, FIELD_COLORMAP
)) { 
 536                 MissingRequired(tif
, "Colormap"); 
 540          * Attempt to deal with a missing StripByteCounts tag. 
 542         if (!TIFFFieldSet(tif
, FIELD_STRIPBYTECOUNTS
)) { 
 544                  * Some manufacturers violate the spec by not giving 
 545                  * the size of the strips.  In this case, assume there 
 546                  * is one uncompressed strip of data. 
 548                 if ((td
->td_planarconfig 
== PLANARCONFIG_CONTIG 
&& 
 549                     td
->td_nstrips 
> 1) || 
 550                     (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE 
&& 
 551                      td
->td_nstrips 
!= td
->td_samplesperpixel
)) { 
 552                     MissingRequired(tif
, "StripByteCounts"); 
 555                 TIFFWarningExt(tif
->tif_clientdata
, module, 
 556                         "%s: TIFF directory is missing required " 
 557                         "\"%s\" field, calculating from imagelength", 
 559                         _TIFFFieldWithTag(tif
,TIFFTAG_STRIPBYTECOUNTS
)->field_name
); 
 560                 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0) 
 563  * Assume we have wrong StripByteCount value (in case of single strip) in 
 565  *   - it is equal to zero along with StripOffset; 
 566  *   - it is larger than file itself (in case of uncompressed image); 
 567  *   - it is smaller than the size of the bytes per row multiplied on the 
 568  *     number of rows.  The last case should not be checked in the case of 
 569  *     writing new image, because we may do not know the exact strip size 
 570  *     until the whole image will be written and directory dumped out. 
 572 #define BYTECOUNTLOOKSBAD \ 
 573     ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \ 
 574       (td->td_compression == COMPRESSION_NONE && \ 
 575        td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \ 
 576       (tif->tif_mode == O_RDONLY && \ 
 577        td->td_compression == COMPRESSION_NONE && \ 
 578        td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) ) 
 580         } else if (td
->td_nstrips 
== 1  
 581                    && td
->td_stripoffset
[0] != 0  
 582                    && BYTECOUNTLOOKSBAD
) { 
 584                  * XXX: Plexus (and others) sometimes give a value of zero for 
 585                  * a tag when they don't know what the correct value is!  Try 
 586                  * and handle the simple case of estimating the size of a one 
 589                 TIFFWarningExt(tif
->tif_clientdata
, module, 
 590         "%s: Bogus \"%s\" field, ignoring and calculating from imagelength", 
 592                             _TIFFFieldWithTag(tif
,TIFFTAG_STRIPBYTECOUNTS
)->field_name
); 
 593                 if(EstimateStripByteCounts(tif
, dir
, dircount
) < 0) 
 595         } else if (td
->td_planarconfig 
== PLANARCONFIG_CONTIG
 
 596                    && td
->td_nstrips 
> 2 
 597                    && td
->td_compression 
== COMPRESSION_NONE
 
 598                    && td
->td_stripbytecount
[0] != td
->td_stripbytecount
[1]) { 
 600                  * XXX: Some vendors fill StripByteCount array with absolutely 
 601                  * wrong values (it can be equal to StripOffset array, for 
 602                  * example). Catch this case here. 
 604                 TIFFWarningExt(tif
->tif_clientdata
, module, 
 605         "%s: Wrong \"%s\" field, ignoring and calculating from imagelength", 
 607                             _TIFFFieldWithTag(tif
,TIFFTAG_STRIPBYTECOUNTS
)->field_name
); 
 608                 if (EstimateStripByteCounts(tif
, dir
, dircount
) < 0) 
 612                 _TIFFfree((char *)dir
); 
 615         if (!TIFFFieldSet(tif
, FIELD_MAXSAMPLEVALUE
)) 
 616                 td
->td_maxsamplevalue 
= (uint16
)((1L<<td
->td_bitspersample
)-1); 
 618          * Setup default compression scheme. 
 622          * XXX: We can optimize checking for the strip bounds using the sorted 
 623          * bytecounts array. See also comments for TIFFAppendToStrip() 
 624          * function in tif_write.c. 
 626         if (td
->td_nstrips 
> 1) { 
 629                 td
->td_stripbytecountsorted 
= 1; 
 630                 for (strip 
= 1; strip 
< td
->td_nstrips
; strip
++) { 
 631                         if (td
->td_stripoffset
[strip 
- 1] > 
 632                             td
->td_stripoffset
[strip
]) { 
 633                                 td
->td_stripbytecountsorted 
= 0; 
 639         if (!TIFFFieldSet(tif
, FIELD_COMPRESSION
)) 
 640                 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
); 
 642          * Some manufacturers make life difficult by writing 
 643          * large amounts of uncompressed data as a single strip. 
 644          * This is contrary to the recommendations of the spec. 
 645          * The following makes an attempt at breaking such images 
 646          * into strips closer to the recommended 8k bytes.  A 
 647          * side effect, however, is that the RowsPerStrip tag 
 648          * value may be changed. 
 650         if (td
->td_nstrips 
== 1 && td
->td_compression 
== COMPRESSION_NONE 
&& 
 651             (tif
->tif_flags 
& (TIFF_STRIPCHOP
|TIFF_ISTILED
)) == TIFF_STRIPCHOP
) 
 652                 ChopUpSingleUncompressedStrip(tif
); 
 655          * Reinitialize i/o since we are starting on a new directory. 
 657         tif
->tif_row 
= (uint32
) -1; 
 658         tif
->tif_curstrip 
= (tstrip_t
) -1; 
 659         tif
->tif_col 
= (uint32
) -1; 
 660         tif
->tif_curtile 
= (ttile_t
) -1; 
 661         tif
->tif_tilesize 
= (tsize_t
) -1; 
 663         tif
->tif_scanlinesize 
= TIFFScanlineSize(tif
); 
 664         if (!tif
->tif_scanlinesize
) { 
 665                 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: cannot handle zero scanline size", 
 671                 tif
->tif_tilesize 
= TIFFTileSize(tif
); 
 672                 if (!tif
->tif_tilesize
) { 
 673                         TIFFErrorExt(tif
->tif_clientdata
, module, "%s: cannot handle zero tile size", 
 678                 if (!TIFFStripSize(tif
)) { 
 679                         TIFFErrorExt(tif
->tif_clientdata
, module, "%s: cannot handle zero strip size", 
 692  * Read custom directory from the arbitarry offset. 
 693  * The code is very similar to TIFFReadDirectory(). 
 696 TIFFReadCustomDirectory(TIFF
* tif
, toff_t diroff
, 
 697                         const TIFFFieldInfo info
[], size_t n
) 
 699         static const char module[] = "TIFFReadCustomDirectory"; 
 701         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 702         TIFFDirEntry 
*dp
, *dir 
= NULL
; 
 703         const TIFFFieldInfo
* fip
; 
 707         _TIFFSetupFieldInfo(tif
, info
, n
); 
 709         tif
->tif_diroff 
= diroff
; 
 711         if (!isMapped(tif
)) { 
 712                 if (!SeekOK(tif
, diroff
)) { 
 713                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 714                             "%s: Seek error accessing TIFF directory", 
 718                 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) { 
 719                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 720                             "%s: Can not read TIFF directory count", 
 724                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 725                         TIFFSwabShort(&dircount
); 
 726                 dir 
= (TIFFDirEntry 
*)_TIFFCheckMalloc(tif
, dircount
, 
 727                                                        sizeof (TIFFDirEntry
), 
 728                                         "to read TIFF custom directory"); 
 731                 if (!ReadOK(tif
, dir
, dircount 
* sizeof (TIFFDirEntry
))) { 
 732                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 733                                   "%.100s: Can not read TIFF directory", 
 740                 if (off 
+ sizeof (uint16
) > tif
->tif_size
) { 
 741                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 742                             "%s: Can not read TIFF directory count", 
 746                         _TIFFmemcpy(&dircount
, tif
->tif_base 
+ off
, sizeof (uint16
)); 
 747                 off 
+= sizeof (uint16
); 
 748                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 749                         TIFFSwabShort(&dircount
); 
 750                 dir 
= (TIFFDirEntry 
*)_TIFFCheckMalloc(tif
, dircount
, 
 751                                                        sizeof (TIFFDirEntry
), 
 752                                         "to read TIFF custom directory"); 
 755                 if (off 
+ dircount 
* sizeof (TIFFDirEntry
) > tif
->tif_size
) { 
 756                         TIFFErrorExt(tif
->tif_clientdata
, module, 
 757                                   "%s: Can not read TIFF directory", 
 761                         _TIFFmemcpy(dir
, tif
->tif_base 
+ off
, 
 762                                     dircount 
* sizeof (TIFFDirEntry
)); 
 766         TIFFFreeDirectory(tif
); 
 769         for (dp 
= dir
, i 
= dircount
; i 
> 0; i
--, dp
++) { 
 770                 if (tif
->tif_flags 
& TIFF_SWAB
) { 
 771                         TIFFSwabArrayOfShort(&dp
->tdir_tag
, 2); 
 772                         TIFFSwabArrayOfLong(&dp
->tdir_count
, 2); 
 775                 if (fix 
>= tif
->tif_nfields 
|| dp
->tdir_tag 
== IGNORE
) 
 778                 while (fix 
< tif
->tif_nfields 
&& 
 779                        tif
->tif_fieldinfo
[fix
]->field_tag 
< dp
->tdir_tag
) 
 782                 if (fix 
>= tif
->tif_nfields 
|| 
 783                     tif
->tif_fieldinfo
[fix
]->field_tag 
!= dp
->tdir_tag
) { 
 785                         TIFFWarningExt(tif
->tif_clientdata
, module, 
 786                         "%s: unknown field with tag %d (0x%x) encountered", 
 787                                     tif
->tif_name
, dp
->tdir_tag
, dp
->tdir_tag
, 
 790                         TIFFMergeFieldInfo(tif
, 
 791                                            _TIFFCreateAnonFieldInfo(tif
, 
 793                                                 (TIFFDataType
)dp
->tdir_type
), 
 797                         while (fix 
< tif
->tif_nfields 
&& 
 798                                tif
->tif_fieldinfo
[fix
]->field_tag 
< dp
->tdir_tag
) 
 802                  * Null out old tags that we ignore. 
 804                 if (tif
->tif_fieldinfo
[fix
]->field_bit 
== FIELD_IGNORE
) { 
 806                         dp
->tdir_tag 
= IGNORE
; 
 812                 fip 
= tif
->tif_fieldinfo
[fix
]; 
 813                 while (dp
->tdir_type 
!= (unsigned short) fip
->field_type
 
 814                        && fix 
< tif
->tif_nfields
) { 
 815                         if (fip
->field_type 
== TIFF_ANY
)        /* wildcard */ 
 817                         fip 
= tif
->tif_fieldinfo
[++fix
]; 
 818                         if (fix 
>= tif
->tif_nfields 
|| 
 819                             fip
->field_tag 
!= dp
->tdir_tag
) { 
 820                                 TIFFWarningExt(tif
->tif_clientdata
, module, 
 821                         "%s: wrong data type %d for \"%s\"; tag ignored", 
 822                                             tif
->tif_name
, dp
->tdir_type
, 
 823                                             tif
->tif_fieldinfo
[fix
-1]->field_name
); 
 828                  * Check count if known in advance. 
 830                 if (fip
->field_readcount 
!= TIFF_VARIABLE
 
 831                     && fip
->field_readcount 
!= TIFF_VARIABLE2
) { 
 832                         uint32 expected 
= (fip
->field_readcount 
== TIFF_SPP
) ? 
 833                             (uint32
) td
->td_samplesperpixel 
: 
 834                             (uint32
) fip
->field_readcount
; 
 835                         if (!CheckDirCount(tif
, dp
, expected
)) 
 839                 (void) TIFFFetchNormalTag(tif
, dp
); 
 853  * EXIF is important special case of custom IFD, so we have a special 
 854  * function to read it. 
 857 TIFFReadEXIFDirectory(TIFF
* tif
, toff_t diroff
) 
 859         size_t exifFieldInfoCount
; 
 860         const TIFFFieldInfo 
*exifFieldInfo 
= 
 861                 _TIFFGetExifFieldInfo(&exifFieldInfoCount
); 
 862         return TIFFReadCustomDirectory(tif
, diroff
, exifFieldInfo
, 
 867 EstimateStripByteCounts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
) 
 869         static const char module[] = "EstimateStripByteCounts"; 
 871         register TIFFDirEntry 
*dp
; 
 872         register TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 875         if (td
->td_stripbytecount
) 
 876                 _TIFFfree(td
->td_stripbytecount
); 
 877         td
->td_stripbytecount 
= (uint32
*) 
 878             _TIFFCheckMalloc(tif
, td
->td_nstrips
, sizeof (uint32
), 
 879                 "for \"StripByteCounts\" array"); 
 880         if (td
->td_compression 
!= COMPRESSION_NONE
) { 
 881                 uint32 space 
= (uint32
)(sizeof (TIFFHeader
) 
 883                     + (dircount 
* sizeof (TIFFDirEntry
)) 
 885                 toff_t filesize 
= TIFFGetFileSize(tif
); 
 888                 /* calculate amount of space used by indirect values */ 
 889                 for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) 
 891                         uint32 cc 
= TIFFDataWidth((TIFFDataType
) dp
->tdir_type
); 
 893                                 TIFFErrorExt(tif
->tif_clientdata
, module, 
 894                         "%s: Cannot determine size of unknown tag type %d", 
 895                                           tif
->tif_name
, dp
->tdir_type
); 
 898                         cc 
= cc 
* dp
->tdir_count
; 
 899                         if (cc 
> sizeof (uint32
)) 
 902                 space 
= filesize 
- space
; 
 903                 if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE
) 
 904                         space 
/= td
->td_samplesperpixel
; 
 905                 for (i 
= 0; i 
< td
->td_nstrips
; i
++) 
 906                         td
->td_stripbytecount
[i
] = space
; 
 908                  * This gross hack handles the case were the offset to 
 909                  * the last strip is past the place where we think the strip 
 910                  * should begin.  Since a strip of data must be contiguous, 
 911                  * it's safe to assume that we've overestimated the amount 
 912                  * of data in the strip and trim this number back accordingly. 
 915                 if (((toff_t
)(td
->td_stripoffset
[i
]+td
->td_stripbytecount
[i
])) 
 917                         td
->td_stripbytecount
[i
] = 
 918                             filesize 
- td
->td_stripoffset
[i
]; 
 920                 uint32 rowbytes 
= TIFFScanlineSize(tif
); 
 921                 uint32 rowsperstrip 
= td
->td_imagelength
/td
->td_stripsperimage
; 
 922                 for (i 
= 0; i 
< td
->td_nstrips
; i
++) 
 923                         td
->td_stripbytecount
[i
] = rowbytes
*rowsperstrip
; 
 925         TIFFSetFieldBit(tif
, FIELD_STRIPBYTECOUNTS
); 
 926         if (!TIFFFieldSet(tif
, FIELD_ROWSPERSTRIP
)) 
 927                 td
->td_rowsperstrip 
= td
->td_imagelength
; 
 932 MissingRequired(TIFF
* tif
, const char* tagname
) 
 934         static const char module[] = "MissingRequired"; 
 936         TIFFErrorExt(tif
->tif_clientdata
, module, 
 937                   "%s: TIFF directory is missing required \"%s\" field", 
 938                   tif
->tif_name
, tagname
); 
 942  * Check the count field of a directory 
 943  * entry against a known value.  The caller 
 944  * is expected to skip/ignore the tag if 
 945  * there is a mismatch. 
 948 CheckDirCount(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 count
) 
 950         if (count 
> dir
->tdir_count
) { 
 951                 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
, 
 952         "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored", 
 953                     _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
, 
 954                     dir
->tdir_count
, count
); 
 956         } else if (count 
< dir
->tdir_count
) { 
 957                 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
, 
 958         "incorrect count for field \"%s\" (%lu, expecting %lu); tag trimmed", 
 959                     _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
, 
 960                     dir
->tdir_count
, count
); 
 967  * Fetch a contiguous directory item. 
 970 TIFFFetchData(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
) 
 972         int w 
= TIFFDataWidth((TIFFDataType
) dir
->tdir_type
); 
 973         tsize_t cc 
= dir
->tdir_count 
* w
; 
 975         /* Check for overflow. */ 
 976         if (!dir
->tdir_count 
|| !w 
|| cc 
/ w 
!= (tsize_t
)dir
->tdir_count
) 
 979         if (!isMapped(tif
)) { 
 980                 if (!SeekOK(tif
, dir
->tdir_offset
)) 
 982                 if (!ReadOK(tif
, cp
, cc
)) 
 985                 /* Check for overflow. */ 
 986                 if ((tsize_t
)dir
->tdir_offset 
+ cc 
< (tsize_t
)dir
->tdir_offset
 
 987                     || (tsize_t
)dir
->tdir_offset 
+ cc 
< cc
 
 988                     || (tsize_t
)dir
->tdir_offset 
+ cc 
> (tsize_t
)tif
->tif_size
) 
 990                 _TIFFmemcpy(cp
, tif
->tif_base 
+ dir
->tdir_offset
, cc
); 
 992         if (tif
->tif_flags 
& TIFF_SWAB
) { 
 993                 switch (dir
->tdir_type
) { 
 996                         TIFFSwabArrayOfShort((uint16
*) cp
, dir
->tdir_count
); 
1001                         TIFFSwabArrayOfLong((uint32
*) cp
, dir
->tdir_count
); 
1004                 case TIFF_SRATIONAL
: 
1005                         TIFFSwabArrayOfLong((uint32
*) cp
, 2*dir
->tdir_count
); 
1008                         TIFFSwabArrayOfDouble((double*) cp
, dir
->tdir_count
); 
1014         TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, 
1015                      "Error fetching data for field \"%s\"", 
1016                      _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1021  * Fetch an ASCII item from the file. 
1024 TIFFFetchString(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
) 
1026         if (dir
->tdir_count 
<= 4) { 
1027                 uint32 l 
= dir
->tdir_offset
; 
1028                 if (tif
->tif_flags 
& TIFF_SWAB
) 
1030                 _TIFFmemcpy(cp
, &l
, dir
->tdir_count
); 
1033         return (TIFFFetchData(tif
, dir
, cp
)); 
1037  * Convert numerator+denominator to float. 
1040 cvtRational(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 num
, uint32 denom
, float* rv
) 
1043                 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, 
1044                     "%s: Rational with zero denominator (num = %lu)", 
1045                     _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
, num
); 
1048                 if (dir
->tdir_type 
== TIFF_RATIONAL
) 
1049                         *rv 
= ((float)num 
/ (float)denom
); 
1051                         *rv 
= ((float)(int32
)num 
/ (float)(int32
)denom
); 
1057  * Fetch a rational item from the file 
1058  * at offset off and return the value 
1059  * as a floating point number. 
1062 TIFFFetchRational(TIFF
* tif
, TIFFDirEntry
* dir
) 
1067         return (!TIFFFetchData(tif
, dir
, (char *)l
) || 
1068             !cvtRational(tif
, dir
, l
[0], l
[1], &v
) ? 1.0f 
: v
); 
1072  * Fetch a single floating point value 
1073  * from the offset field and return it 
1074  * as a native float. 
1077 TIFFFetchFloat(TIFF
* tif
, TIFFDirEntry
* dir
) 
1080         int32 l 
= TIFFExtractData(tif
, dir
->tdir_type
, dir
->tdir_offset
); 
1081         _TIFFmemcpy(&v
, &l
, sizeof(float)); 
1082         TIFFCvtIEEEFloatToNative(tif
, 1, &v
); 
1087  * Fetch an array of BYTE or SBYTE values. 
1090 TIFFFetchByteArray(TIFF
* tif
, TIFFDirEntry
* dir
, uint8
* v
) 
1092     if (dir
->tdir_count 
<= 4) { 
1094          * Extract data from offset field. 
1096         if (tif
->tif_header
.tiff_magic 
== TIFF_BIGENDIAN
) { 
1097             if (dir
->tdir_type 
== TIFF_SBYTE
) 
1098                 switch (dir
->tdir_count
) { 
1099                     case 4: v
[3] = dir
->tdir_offset 
& 0xff; 
1100                     case 3: v
[2] = (dir
->tdir_offset 
>> 8) & 0xff; 
1101                     case 2: v
[1] = (dir
->tdir_offset 
>> 16) & 0xff; 
1102                     case 1: v
[0] = dir
->tdir_offset 
>> 24; 
1105                 switch (dir
->tdir_count
) { 
1106                     case 4: v
[3] = dir
->tdir_offset 
& 0xff; 
1107                     case 3: v
[2] = (dir
->tdir_offset 
>> 8) & 0xff; 
1108                     case 2: v
[1] = (dir
->tdir_offset 
>> 16) & 0xff; 
1109                     case 1: v
[0] = dir
->tdir_offset 
>> 24; 
1112             if (dir
->tdir_type 
== TIFF_SBYTE
) 
1113                 switch (dir
->tdir_count
) { 
1114                     case 4: v
[3] = dir
->tdir_offset 
>> 24; 
1115                     case 3: v
[2] = (dir
->tdir_offset 
>> 16) & 0xff; 
1116                     case 2: v
[1] = (dir
->tdir_offset 
>> 8) & 0xff; 
1117                     case 1: v
[0] = dir
->tdir_offset 
& 0xff; 
1120                 switch (dir
->tdir_count
) { 
1121                     case 4: v
[3] = dir
->tdir_offset 
>> 24; 
1122                     case 3: v
[2] = (dir
->tdir_offset 
>> 16) & 0xff; 
1123                     case 2: v
[1] = (dir
->tdir_offset 
>> 8) & 0xff; 
1124                     case 1: v
[0] = dir
->tdir_offset 
& 0xff; 
1129         return (TIFFFetchData(tif
, dir
, (char*) v
) != 0);       /* XXX */ 
1133  * Fetch an array of SHORT or SSHORT values. 
1136 TIFFFetchShortArray(TIFF
* tif
, TIFFDirEntry
* dir
, uint16
* v
) 
1138         if (dir
->tdir_count 
<= 2) { 
1139                 if (tif
->tif_header
.tiff_magic 
== TIFF_BIGENDIAN
) { 
1140                         switch (dir
->tdir_count
) { 
1141                         case 2: v
[1] = (uint16
) (dir
->tdir_offset 
& 0xffff); 
1142                         case 1: v
[0] = (uint16
) (dir
->tdir_offset 
>> 16); 
1145                         switch (dir
->tdir_count
) { 
1146                         case 2: v
[1] = (uint16
) (dir
->tdir_offset 
>> 16); 
1147                         case 1: v
[0] = (uint16
) (dir
->tdir_offset 
& 0xffff); 
1152                 return (TIFFFetchData(tif
, dir
, (char *)v
) != 0); 
1156  * Fetch a pair of SHORT or BYTE values. Some tags may have either BYTE 
1157  * or SHORT type and this function works with both ones. 
1160 TIFFFetchShortPair(TIFF
* tif
, TIFFDirEntry
* dir
) 
1162         switch (dir
->tdir_type
) { 
1167                         return TIFFFetchByteArray(tif
, dir
, v
) 
1168                                 && TIFFSetField(tif
, dir
->tdir_tag
, v
[0], v
[1]); 
1174                         return TIFFFetchShortArray(tif
, dir
, v
) 
1175                                 && TIFFSetField(tif
, dir
->tdir_tag
, v
[0], v
[1]); 
1183  * Fetch an array of LONG or SLONG values. 
1186 TIFFFetchLongArray(TIFF
* tif
, TIFFDirEntry
* dir
, uint32
* v
) 
1188         if (dir
->tdir_count 
== 1) { 
1189                 v
[0] = dir
->tdir_offset
; 
1192                 return (TIFFFetchData(tif
, dir
, (char*) v
) != 0); 
1196  * Fetch an array of RATIONAL or SRATIONAL values. 
1199 TIFFFetchRationalArray(TIFF
* tif
, TIFFDirEntry
* dir
, float* v
) 
1204         l 
= (uint32
*)_TIFFCheckMalloc(tif
, 
1205             dir
->tdir_count
, TIFFDataWidth((TIFFDataType
) dir
->tdir_type
), 
1206             "to fetch array of rationals"); 
1208                 if (TIFFFetchData(tif
, dir
, (char *)l
)) { 
1210                         for (i 
= 0; i 
< dir
->tdir_count
; i
++) { 
1211                                 ok 
= cvtRational(tif
, dir
, 
1212                                     l
[2*i
+0], l
[2*i
+1], &v
[i
]); 
1217                 _TIFFfree((char *)l
); 
1223  * Fetch an array of FLOAT values. 
1226 TIFFFetchFloatArray(TIFF
* tif
, TIFFDirEntry
* dir
, float* v
) 
1229         if (dir
->tdir_count 
== 1) { 
1230                 v
[0] = *(float*) &dir
->tdir_offset
; 
1231                 TIFFCvtIEEEFloatToNative(tif
, dir
->tdir_count
, v
); 
1233         } else  if (TIFFFetchData(tif
, dir
, (char*) v
)) { 
1234                 TIFFCvtIEEEFloatToNative(tif
, dir
->tdir_count
, v
); 
1241  * Fetch an array of DOUBLE values. 
1244 TIFFFetchDoubleArray(TIFF
* tif
, TIFFDirEntry
* dir
, double* v
) 
1246         if (TIFFFetchData(tif
, dir
, (char*) v
)) { 
1247                 TIFFCvtIEEEDoubleToNative(tif
, dir
->tdir_count
, v
); 
1254  * Fetch an array of ANY values.  The actual values are 
1255  * returned as doubles which should be able hold all the 
1256  * types.  Yes, there really should be an tany_t to avoid 
1257  * this potential non-portability ...  Note in particular 
1258  * that we assume that the double return value vector is 
1259  * large enough to read in any fundamental type.  We use 
1260  * that vector as a buffer to read in the base type vector 
1261  * and then convert it in place to double (from end 
1262  * to front of course). 
1265 TIFFFetchAnyArray(TIFF
* tif
, TIFFDirEntry
* dir
, double* v
) 
1269         switch (dir
->tdir_type
) { 
1272                 if (!TIFFFetchByteArray(tif
, dir
, (uint8
*) v
)) 
1274                 if (dir
->tdir_type 
== TIFF_BYTE
) { 
1275                         uint8
* vp 
= (uint8
*) v
; 
1276                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1279                         int8
* vp 
= (int8
*) v
; 
1280                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1286                 if (!TIFFFetchShortArray(tif
, dir
, (uint16
*) v
)) 
1288                 if (dir
->tdir_type 
== TIFF_SHORT
) { 
1289                         uint16
* vp 
= (uint16
*) v
; 
1290                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1293                         int16
* vp 
= (int16
*) v
; 
1294                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1300                 if (!TIFFFetchLongArray(tif
, dir
, (uint32
*) v
)) 
1302                 if (dir
->tdir_type 
== TIFF_LONG
) { 
1303                         uint32
* vp 
= (uint32
*) v
; 
1304                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1307                         int32
* vp 
= (int32
*) v
; 
1308                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1313         case TIFF_SRATIONAL
: 
1314                 if (!TIFFFetchRationalArray(tif
, dir
, (float*) v
)) 
1316                 { float* vp 
= (float*) v
; 
1317                   for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1322                 if (!TIFFFetchFloatArray(tif
, dir
, (float*) v
)) 
1324                 { float* vp 
= (float*) v
; 
1325                   for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
1330                 return (TIFFFetchDoubleArray(tif
, dir
, (double*) v
)); 
1334                 /* TIFF_UNDEFINED */ 
1335                 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, 
1336                              "cannot read TIFF_ANY type %d for field \"%s\"", 
1338                              _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1345  * Fetch a tag that is not handled by special case code. 
1348 TIFFFetchNormalTag(TIFF
* tif
, TIFFDirEntry
* dp
) 
1350         static const char mesg
[] = "to fetch tag value"; 
1352         const TIFFFieldInfo
* fip 
= _TIFFFieldWithTag(tif
, dp
->tdir_tag
); 
1354         if (dp
->tdir_count 
> 1) {               /* array of values */ 
1357                 switch (dp
->tdir_type
) { 
1360                         cp 
= (char *)_TIFFCheckMalloc(tif
, 
1361                             dp
->tdir_count
, sizeof (uint8
), mesg
); 
1362                         ok 
= cp 
&& TIFFFetchByteArray(tif
, dp
, (uint8
*) cp
); 
1366                         cp 
= (char *)_TIFFCheckMalloc(tif
, 
1367                             dp
->tdir_count
, sizeof (uint16
), mesg
); 
1368                         ok 
= cp 
&& TIFFFetchShortArray(tif
, dp
, (uint16
*) cp
); 
1372                         cp 
= (char *)_TIFFCheckMalloc(tif
, 
1373                             dp
->tdir_count
, sizeof (uint32
), mesg
); 
1374                         ok 
= cp 
&& TIFFFetchLongArray(tif
, dp
, (uint32
*) cp
); 
1377                 case TIFF_SRATIONAL
: 
1378                         cp 
= (char *)_TIFFCheckMalloc(tif
, 
1379                             dp
->tdir_count
, sizeof (float), mesg
); 
1380                         ok 
= cp 
&& TIFFFetchRationalArray(tif
, dp
, (float*) cp
); 
1383                         cp 
= (char *)_TIFFCheckMalloc(tif
, 
1384                             dp
->tdir_count
, sizeof (float), mesg
); 
1385                         ok 
= cp 
&& TIFFFetchFloatArray(tif
, dp
, (float*) cp
); 
1388                         cp 
= (char *)_TIFFCheckMalloc(tif
, 
1389                             dp
->tdir_count
, sizeof (double), mesg
); 
1390                         ok 
= cp 
&& TIFFFetchDoubleArray(tif
, dp
, (double*) cp
); 
1393                 case TIFF_UNDEFINED
:            /* bit of a cheat... */ 
1395                          * Some vendors write strings w/o the trailing 
1396                          * NULL byte, so always append one just in case. 
1398                         cp 
= (char *)_TIFFCheckMalloc(tif
, dp
->tdir_count 
+ 1, 
1400                         if( (ok 
= (cp 
&& TIFFFetchString(tif
, dp
, cp
))) != 0 ) 
1401                                 cp
[dp
->tdir_count
] = '\0';      /* XXX */ 
1405                         ok 
= (fip
->field_passcount 
? 
1406                             TIFFSetField(tif
, dp
->tdir_tag
, dp
->tdir_count
, cp
) 
1407                           : TIFFSetField(tif
, dp
->tdir_tag
, cp
)); 
1411         } else if (CheckDirCount(tif
, dp
, 1)) { /* singleton value */ 
1412                 switch (dp
->tdir_type
) { 
1418                          * If the tag is also acceptable as a LONG or SLONG 
1419                          * then TIFFSetField will expect an uint32 parameter 
1420                          * passed to it (through varargs).  Thus, for machines 
1421                          * where sizeof (int) != sizeof (uint32) we must do 
1422                          * a careful check here.  It's hard to say if this 
1423                          * is worth optimizing. 
1425                          * NB: We use TIFFFieldWithTag here knowing that 
1426                          *     it returns us the first entry in the table 
1427                          *     for the tag and that that entry is for the 
1428                          *     widest potential data type the tag may have. 
1430                         { TIFFDataType type 
= fip
->field_type
; 
1431                           if (type 
!= TIFF_LONG 
&& type 
!= TIFF_SLONG
) { 
1433                            TIFFExtractData(tif
, dp
->tdir_type
, dp
->tdir_offset
); 
1434                                 ok 
= (fip
->field_passcount 
? 
1435                                     TIFFSetField(tif
, dp
->tdir_tag
, 1, &v
) 
1436                                   : TIFFSetField(tif
, dp
->tdir_tag
, v
)); 
1444                     TIFFExtractData(tif
, dp
->tdir_type
, dp
->tdir_offset
); 
1445                           ok 
= (fip
->field_passcount 
?  
1446                               TIFFSetField(tif
, dp
->tdir_tag
, 1, &v32
) 
1447                             : TIFFSetField(tif
, dp
->tdir_tag
, v32
)); 
1451                 case TIFF_SRATIONAL
: 
1453                         { float v 
= (dp
->tdir_type 
== TIFF_FLOAT 
?  
1454                               TIFFFetchFloat(tif
, dp
) 
1455                             : TIFFFetchRational(tif
, dp
)); 
1456                           ok 
= (fip
->field_passcount 
? 
1457                               TIFFSetField(tif
, dp
->tdir_tag
, 1, &v
) 
1458                             : TIFFSetField(tif
, dp
->tdir_tag
, v
)); 
1463                           ok 
= (TIFFFetchDoubleArray(tif
, dp
, &v
) && 
1464                             (fip
->field_passcount 
? 
1465                               TIFFSetField(tif
, dp
->tdir_tag
, 1, &v
) 
1466                             : TIFFSetField(tif
, dp
->tdir_tag
, v
)) 
1471                 case TIFF_UNDEFINED
:            /* bit of a cheat... */ 
1473                           if( (ok 
= (TIFFFetchString(tif
, dp
, c
) != 0)) != 0 ) { 
1474                                 c
[1] = '\0';            /* XXX paranoid */ 
1475                                 ok 
= (fip
->field_passcount 
? 
1476                                         TIFFSetField(tif
, dp
->tdir_tag
, 1, c
) 
1477                                       : TIFFSetField(tif
, dp
->tdir_tag
, c
)); 
1486 #define NITEMS(x)       (sizeof (x) / sizeof (x[0])) 
1488  * Fetch samples/pixel short values for  
1489  * the specified tag and verify that 
1490  * all values are the same. 
1493 TIFFFetchPerSampleShorts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16
* pl
) 
1495     uint16 samples 
= tif
->tif_dir
.td_samplesperpixel
; 
1498     if (CheckDirCount(tif
, dir
, (uint32
) samples
)) { 
1502         if (dir
->tdir_count 
> NITEMS(buf
)) 
1503             v 
= (uint16
*) _TIFFCheckMalloc(tif
, dir
->tdir_count
, sizeof(uint16
), 
1504                                       "to fetch per-sample values"); 
1505         if (v 
&& TIFFFetchShortArray(tif
, dir
, v
)) { 
1507             int check_count 
= dir
->tdir_count
; 
1508             if( samples 
< check_count 
) 
1509                 check_count 
= samples
; 
1511             for (i 
= 1; i 
< check_count
; i
++) 
1513                                         TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, 
1514                               "Cannot handle different per-sample values for field \"%s\"", 
1515                               _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1529  * Fetch samples/pixel long values for  
1530  * the specified tag and verify that 
1531  * all values are the same. 
1534 TIFFFetchPerSampleLongs(TIFF
* tif
, TIFFDirEntry
* dir
, uint32
* pl
) 
1536     uint16 samples 
= tif
->tif_dir
.td_samplesperpixel
; 
1539     if (CheckDirCount(tif
, dir
, (uint32
) samples
)) { 
1543         if (dir
->tdir_count 
> NITEMS(buf
)) 
1544             v 
= (uint32
*) _TIFFCheckMalloc(tif
, dir
->tdir_count
, sizeof(uint32
), 
1545                                       "to fetch per-sample values"); 
1546         if (v 
&& TIFFFetchLongArray(tif
, dir
, v
)) { 
1548             int check_count 
= dir
->tdir_count
; 
1550             if( samples 
< check_count 
) 
1551                 check_count 
= samples
; 
1552             for (i 
= 1; i 
< check_count
; i
++) 
1554                                         TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, 
1555                               "Cannot handle different per-sample values for field \"%s\"", 
1556                               _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1570  * Fetch samples/pixel ANY values for the specified tag and verify that all 
1571  * values are the same. 
1574 TIFFFetchPerSampleAnys(TIFF
* tif
, TIFFDirEntry
* dir
, double* pl
) 
1576     uint16 samples 
= tif
->tif_dir
.td_samplesperpixel
; 
1579     if (CheckDirCount(tif
, dir
, (uint32
) samples
)) { 
1583         if (dir
->tdir_count 
> NITEMS(buf
)) 
1584             v 
= (double*) _TIFFCheckMalloc(tif
, dir
->tdir_count
, sizeof (double), 
1585                                       "to fetch per-sample values"); 
1586         if (v 
&& TIFFFetchAnyArray(tif
, dir
, v
)) { 
1588             int check_count 
= dir
->tdir_count
; 
1589             if( samples 
< check_count 
) 
1590                 check_count 
= samples
; 
1592             for (i 
= 1; i 
< check_count
; i
++) 
1594                     TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, 
1595                               "Cannot handle different per-sample values for field \"%s\"", 
1596                               _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1611  * Fetch a set of offsets or lengths. 
1612  * While this routine says "strips", in fact it's also used for tiles. 
1615 TIFFFetchStripThing(TIFF
* tif
, TIFFDirEntry
* dir
, long nstrips
, uint32
** lpp
) 
1617         register uint32
* lp
; 
1620         CheckDirCount(tif
, dir
, (uint32
) nstrips
); 
1623          * Allocate space for strip information. 
1626             (*lpp 
= (uint32 
*)_TIFFCheckMalloc(tif
, 
1627               nstrips
, sizeof (uint32
), "for strip array")) == NULL
) 
1630         _TIFFmemset( lp
, 0, sizeof(uint32
) * nstrips 
); 
1632         if (dir
->tdir_type 
== (int)TIFF_SHORT
) { 
1634                  * Handle uint16->uint32 expansion. 
1636                 uint16
* dp 
= (uint16
*) _TIFFCheckMalloc(tif
, 
1637                     dir
->tdir_count
, sizeof (uint16
), "to fetch strip tag"); 
1640                 if( (status 
= TIFFFetchShortArray(tif
, dir
, dp
)) != 0 ) { 
1643                     for( i 
= 0; i 
< nstrips 
&& i 
< (int) dir
->tdir_count
; i
++ ) 
1648                 _TIFFfree((char*) dp
); 
1650         } else if( nstrips 
!= (int) dir
->tdir_count 
) { 
1651             /* Special case to correct length */ 
1653             uint32
* dp 
= (uint32
*) _TIFFCheckMalloc(tif
, 
1654                     dir
->tdir_count
, sizeof (uint32
), "to fetch strip tag"); 
1658             status 
= TIFFFetchLongArray(tif
, dir
, dp
); 
1662                 for( i 
= 0; i 
< nstrips 
&& i 
< (int) dir
->tdir_count
; i
++ ) 
1668             _TIFFfree( (char *) dp 
); 
1670             status 
= TIFFFetchLongArray(tif
, dir
, lp
); 
1676  * Fetch and set the RefBlackWhite tag. 
1679 TIFFFetchRefBlackWhite(TIFF
* tif
, TIFFDirEntry
* dir
) 
1681         static const char mesg
[] = "for \"ReferenceBlackWhite\" array"; 
1685         if (dir
->tdir_type 
== TIFF_RATIONAL
) 
1686                 return (TIFFFetchNormalTag(tif
, dir
)); 
1688          * Handle LONG's for backward compatibility. 
1690         cp 
= (char *)_TIFFCheckMalloc(tif
, dir
->tdir_count
, 
1691                                       sizeof (uint32
), mesg
); 
1692         if( (ok 
= (cp 
&& TIFFFetchLongArray(tif
, dir
, (uint32
*) cp
))) != 0) { 
1693                 float* fp 
= (float*) 
1694                     _TIFFCheckMalloc(tif
, dir
->tdir_count
, sizeof (float), mesg
); 
1695                 if( (ok 
= (fp 
!= NULL
)) != 0 ) { 
1697                         for (i 
= 0; i 
< dir
->tdir_count
; i
++) 
1698                                 fp
[i
] = (float)((uint32
*) cp
)[i
]; 
1699                         ok 
= TIFFSetField(tif
, dir
->tdir_tag
, fp
); 
1700                         _TIFFfree((char*) fp
); 
1709  * Replace a single strip (tile) of uncompressed data by 
1710  * multiple strips (tiles), each approximately 8Kbytes. 
1711  * This is useful for dealing with large images or 
1712  * for dealing with machines with a limited amount 
1716 ChopUpSingleUncompressedStrip(TIFF
* tif
) 
1718         register TIFFDirectory 
*td 
= &tif
->tif_dir
; 
1719         uint32 bytecount 
= td
->td_stripbytecount
[0]; 
1720         uint32 offset 
= td
->td_stripoffset
[0]; 
1721         tsize_t rowbytes 
= TIFFVTileSize(tif
, 1), stripbytes
; 
1722         tstrip_t strip
, nstrips
, rowsperstrip
; 
1727          * Make the rows hold at least one scanline, but fill specified amount 
1728          * of data if possible. 
1730         if (rowbytes 
> STRIP_SIZE_DEFAULT
) { 
1731                 stripbytes 
= rowbytes
; 
1733         } else if (rowbytes 
> 0 ) { 
1734                 rowsperstrip 
= STRIP_SIZE_DEFAULT 
/ rowbytes
; 
1735                 stripbytes 
= rowbytes 
* rowsperstrip
; 
1741          * never increase the number of strips in an image 
1743         if (rowsperstrip 
>= td
->td_rowsperstrip
) 
1745         nstrips 
= (tstrip_t
) TIFFhowmany(bytecount
, stripbytes
); 
1746         if( nstrips 
== 0 ) /* something is wonky, do nothing. */ 
1749         newcounts 
= (uint32
*) _TIFFCheckMalloc(tif
, nstrips
, sizeof (uint32
), 
1750                                 "for chopped \"StripByteCounts\" array"); 
1751         newoffsets 
= (uint32
*) _TIFFCheckMalloc(tif
, nstrips
, sizeof (uint32
), 
1752                                 "for chopped \"StripOffsets\" array"); 
1753         if (newcounts 
== NULL 
|| newoffsets 
== NULL
) { 
1755                  * Unable to allocate new strip information, give 
1756                  * up and use the original one strip information. 
1758                 if (newcounts 
!= NULL
) 
1759                         _TIFFfree(newcounts
); 
1760                 if (newoffsets 
!= NULL
) 
1761                         _TIFFfree(newoffsets
); 
1765          * Fill the strip information arrays with new bytecounts and offsets 
1766          * that reflect the broken-up format. 
1768         for (strip 
= 0; strip 
< nstrips
; strip
++) { 
1769                 if (stripbytes 
> (tsize_t
) bytecount
) 
1770                         stripbytes 
= bytecount
; 
1771                 newcounts
[strip
] = stripbytes
; 
1772                 newoffsets
[strip
] = offset
; 
1773                 offset 
+= stripbytes
; 
1774                 bytecount 
-= stripbytes
; 
1777          * Replace old single strip info with multi-strip info. 
1779         td
->td_stripsperimage 
= td
->td_nstrips 
= nstrips
; 
1780         TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rowsperstrip
); 
1782         _TIFFfree(td
->td_stripbytecount
); 
1783         _TIFFfree(td
->td_stripoffset
); 
1784         td
->td_stripbytecount 
= newcounts
; 
1785         td
->td_stripoffset 
= newoffsets
; 
1786         td
->td_stripbytecountsorted 
= 1; 
1789 /* vim: set ts=8 sts=8 sw=8 noet: */