LCOV - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_dirread.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 2600 646 24.8 %
Date: 2012-12-26 Functions: 89 30 33.7 %

       1                 : /* $Id: tif_dirread.c,v 1.178 2012-08-19 16:56:34 bfriesen Exp $ */
       2                 : 
       3                 : /*
       4                 :  * Copyright (c) 1988-1997 Sam Leffler
       5                 :  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
       6                 :  *
       7                 :  * Permission to use, copy, modify, distribute, and sell this software and
       8                 :  * its documentation for any purpose is hereby granted without fee, provided
       9                 :  * that (i) the above copyright notices and this permission notice appear in
      10                 :  * all copies of the software and related documentation, and (ii) the names of
      11                 :  * Sam Leffler and Silicon Graphics may not be used in any advertising or
      12                 :  * publicity relating to the software without the specific, prior written
      13                 :  * permission of Sam Leffler and Silicon Graphics.
      14                 :  *
      15                 :  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
      16                 :  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
      17                 :  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
      18                 :  *
      19                 :  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
      20                 :  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
      21                 :  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
      22                 :  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
      23                 :  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
      24                 :  * OF THIS SOFTWARE.
      25                 :  */
      26                 : 
      27                 : /*
      28                 :  * TIFF Library.
      29                 :  *
      30                 :  * Directory Read Support Routines.
      31                 :  */
      32                 : 
      33                 : /* Suggested pending improvements:
      34                 :  * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
      35                 :  *   eliminating current use of the IGNORE value, and therefore eliminating
      36                 :  *   current irrational behaviour on tags with tag id code 0
      37                 :  * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
      38                 :  *   the pointer to the appropriate TIFFField structure early on in
      39                 :  *   TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
      40                 :  */
      41                 : 
      42                 : #include "tiffiop.h"
      43                 : 
      44                 : #define IGNORE 0          /* tag placeholder used below */
      45                 : #define FAILED_FII    ((uint32) -1)
      46                 : 
      47                 : #ifdef HAVE_IEEEFP
      48                 : # define TIFFCvtIEEEFloatToNative(tif, n, fp)
      49                 : # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
      50                 : #else
      51                 : extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
      52                 : extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
      53                 : #endif
      54                 : 
      55                 : enum TIFFReadDirEntryErr {
      56                 :   TIFFReadDirEntryErrOk = 0,
      57                 :   TIFFReadDirEntryErrCount = 1,
      58                 :   TIFFReadDirEntryErrType = 2,
      59                 :   TIFFReadDirEntryErrIo = 3,
      60                 :   TIFFReadDirEntryErrRange = 4,
      61                 :   TIFFReadDirEntryErrPsdif = 5,
      62                 :   TIFFReadDirEntryErrSizesan = 6,
      63                 :   TIFFReadDirEntryErrAlloc = 7,
      64                 : };
      65                 : 
      66                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
      67                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
      68                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
      69                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
      70                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
      71                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
      72                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
      73                 : 
      74                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
      75                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
      76                 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
      77                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
      78                 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
      79                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
      80                 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
      81                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
      82                 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
      83                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
      84                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
      85                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
      86                 : 
      87                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
      88                 : #if 0
      89                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
      90                 : #endif
      91                 : 
      92                 : static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
      93                 : static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
      94                 : static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
      95                 : static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
      96                 : static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
      97                 : static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
      98                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
      99                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
     100                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
     101                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
     102                 : static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
     103                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
     104                 : 
     105                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
     106                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
     107                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
     108                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
     109                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
     110                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
     111                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
     112                 : 
     113                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
     114                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
     115                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
     116                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
     117                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
     118                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
     119                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
     120                 : 
     121                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
     122                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
     123                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
     124                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
     125                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
     126                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
     127                 : 
     128                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
     129                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
     130                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
     131                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
     132                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
     133                 : 
     134                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
     135                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
     136                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
     137                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
     138                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
     139                 : 
     140                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
     141                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
     142                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
     143                 : 
     144                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
     145                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
     146                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
     147                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
     148                 : 
     149                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
     150                 : 
     151                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
     152                 : static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
     153                 : 
     154                 : static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
     155                 : static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
     156                 : static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
     157                 : 
     158                 : static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
     159                 : static void MissingRequired(TIFF*, const char*);
     160                 : static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
     161                 : static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
     162                 : static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
     163                 : static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
     164                 : static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
     165                 : static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
     166                 : static void ChopUpSingleUncompressedStrip(TIFF*);
     167                 : static uint64 TIFFReadUInt64(const uint8 *value);
     168                 : 
     169                 : typedef union _UInt64Aligned_t
     170                 : {
     171                 :         double d;
     172                 :   uint64 l;
     173                 :   uint32 i[2];
     174                 :   uint16 s[4];
     175                 :   uint8  c[8];
     176                 : } UInt64Aligned_t;
     177                 : 
     178                 : /*
     179                 :   Unaligned safe copy of a uint64 value from an octet array.
     180                 : */
     181            2420 : static uint64 TIFFReadUInt64(const uint8 *value)
     182                 : {
     183                 :   UInt64Aligned_t result;
     184                 : 
     185            2420 :   result.c[0]=value[0];
     186            2420 :   result.c[1]=value[1];
     187            2420 :   result.c[2]=value[2];
     188            2420 :   result.c[3]=value[3];
     189            2420 :   result.c[4]=value[4];
     190            2420 :   result.c[5]=value[5];
     191            2420 :   result.c[6]=value[6];
     192            2420 :   result.c[7]=value[7];
     193                 : 
     194            2420 :   return result.l;
     195                 : }
     196                 : 
     197               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
     198                 : {
     199                 :   enum TIFFReadDirEntryErr err;
     200               0 :   if (direntry->tdir_count!=1)
     201               0 :     return(TIFFReadDirEntryErrCount);
     202               0 :   switch (direntry->tdir_type)
     203                 :   {
     204                 :     case TIFF_BYTE:
     205               0 :       TIFFReadDirEntryCheckedByte(tif,direntry,value);
     206               0 :       return(TIFFReadDirEntryErrOk);
     207                 :     case TIFF_SBYTE:
     208                 :       {
     209                 :         int8 m;
     210               0 :         TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
     211               0 :         err=TIFFReadDirEntryCheckRangeByteSbyte(m);
     212               0 :         if (err!=TIFFReadDirEntryErrOk)
     213               0 :           return(err);
     214               0 :         *value=(uint8)m;
     215               0 :         return(TIFFReadDirEntryErrOk);
     216                 :       }
     217                 :     case TIFF_SHORT:
     218                 :       {
     219                 :         uint16 m;
     220               0 :         TIFFReadDirEntryCheckedShort(tif,direntry,&m);
     221               0 :         err=TIFFReadDirEntryCheckRangeByteShort(m);
     222               0 :         if (err!=TIFFReadDirEntryErrOk)
     223               0 :           return(err);
     224               0 :         *value=(uint8)m;
     225               0 :         return(TIFFReadDirEntryErrOk);
     226                 :       }
     227                 :     case TIFF_SSHORT:
     228                 :       {
     229                 :         int16 m;
     230               0 :         TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
     231               0 :         err=TIFFReadDirEntryCheckRangeByteSshort(m);
     232               0 :         if (err!=TIFFReadDirEntryErrOk)
     233               0 :           return(err);
     234               0 :         *value=(uint8)m;
     235               0 :         return(TIFFReadDirEntryErrOk);
     236                 :       }
     237                 :     case TIFF_LONG:
     238                 :       {
     239                 :         uint32 m;
     240               0 :         TIFFReadDirEntryCheckedLong(tif,direntry,&m);
     241               0 :         err=TIFFReadDirEntryCheckRangeByteLong(m);
     242               0 :         if (err!=TIFFReadDirEntryErrOk)
     243               0 :           return(err);
     244               0 :         *value=(uint8)m;
     245               0 :         return(TIFFReadDirEntryErrOk);
     246                 :       }
     247                 :     case TIFF_SLONG:
     248                 :       {
     249                 :         int32 m;
     250               0 :         TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
     251               0 :         err=TIFFReadDirEntryCheckRangeByteSlong(m);
     252               0 :         if (err!=TIFFReadDirEntryErrOk)
     253               0 :           return(err);
     254               0 :         *value=(uint8)m;
     255               0 :         return(TIFFReadDirEntryErrOk);
     256                 :       }
     257                 :     case TIFF_LONG8:
     258                 :       {
     259                 :         uint64 m;
     260               0 :         err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
     261               0 :         if (err!=TIFFReadDirEntryErrOk)
     262               0 :           return(err);
     263               0 :         err=TIFFReadDirEntryCheckRangeByteLong8(m);
     264               0 :         if (err!=TIFFReadDirEntryErrOk)
     265               0 :           return(err);
     266               0 :         *value=(uint8)m;
     267               0 :         return(TIFFReadDirEntryErrOk);
     268                 :       }
     269                 :     case TIFF_SLONG8:
     270                 :       {
     271                 :         int64 m;
     272               0 :         err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
     273               0 :         if (err!=TIFFReadDirEntryErrOk)
     274               0 :           return(err);
     275               0 :         err=TIFFReadDirEntryCheckRangeByteSlong8(m);
     276               0 :         if (err!=TIFFReadDirEntryErrOk)
     277               0 :           return(err);
     278               0 :         *value=(uint8)m;
     279               0 :         return(TIFFReadDirEntryErrOk);
     280                 :       }
     281                 :     default:
     282               0 :       return(TIFFReadDirEntryErrType);
     283                 :   }
     284                 : }
     285                 : 
     286           61641 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
     287                 : {
     288                 :   enum TIFFReadDirEntryErr err;
     289           61641 :   if (direntry->tdir_count!=1)
     290            3672 :     return(TIFFReadDirEntryErrCount);
     291           57969 :   switch (direntry->tdir_type)
     292                 :   {
     293                 :     case TIFF_BYTE:
     294                 :       {
     295                 :         uint8 m;
     296               0 :         TIFFReadDirEntryCheckedByte(tif,direntry,&m);
     297               0 :         *value=(uint16)m;
     298               0 :         return(TIFFReadDirEntryErrOk);
     299                 :       }
     300                 :     case TIFF_SBYTE:
     301                 :       {
     302                 :         int8 m;
     303               0 :         TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
     304               0 :         err=TIFFReadDirEntryCheckRangeShortSbyte(m);
     305               0 :         if (err!=TIFFReadDirEntryErrOk)
     306               0 :           return(err);
     307               0 :         *value=(uint16)m;
     308               0 :         return(TIFFReadDirEntryErrOk);
     309                 :       }
     310                 :     case TIFF_SHORT:
     311           57968 :       TIFFReadDirEntryCheckedShort(tif,direntry,value);
     312           57968 :       return(TIFFReadDirEntryErrOk);
     313                 :     case TIFF_SSHORT:
     314                 :       {
     315                 :         int16 m;
     316               0 :         TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
     317               0 :         err=TIFFReadDirEntryCheckRangeShortSshort(m);
     318               0 :         if (err!=TIFFReadDirEntryErrOk)
     319               0 :           return(err);
     320               0 :         *value=(uint16)m;
     321               0 :         return(TIFFReadDirEntryErrOk);
     322                 :       }
     323                 :     case TIFF_LONG:
     324                 :       {
     325                 :         uint32 m;
     326               1 :         TIFFReadDirEntryCheckedLong(tif,direntry,&m);
     327               1 :         err=TIFFReadDirEntryCheckRangeShortLong(m);
     328               1 :         if (err!=TIFFReadDirEntryErrOk)
     329               0 :           return(err);
     330               1 :         *value=(uint16)m;
     331               1 :         return(TIFFReadDirEntryErrOk);
     332                 :       }
     333                 :     case TIFF_SLONG:
     334                 :       {
     335                 :         int32 m;
     336               0 :         TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
     337               0 :         err=TIFFReadDirEntryCheckRangeShortSlong(m);
     338               0 :         if (err!=TIFFReadDirEntryErrOk)
     339               0 :           return(err);
     340               0 :         *value=(uint16)m;
     341               0 :         return(TIFFReadDirEntryErrOk);
     342                 :       }
     343                 :     case TIFF_LONG8:
     344                 :       {
     345                 :         uint64 m;
     346               0 :         err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
     347               0 :         if (err!=TIFFReadDirEntryErrOk)
     348               0 :           return(err);
     349               0 :         err=TIFFReadDirEntryCheckRangeShortLong8(m);
     350               0 :         if (err!=TIFFReadDirEntryErrOk)
     351               0 :           return(err);
     352               0 :         *value=(uint16)m;
     353               0 :         return(TIFFReadDirEntryErrOk);
     354                 :       }
     355                 :     case TIFF_SLONG8:
     356                 :       {
     357                 :         int64 m;
     358               0 :         err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
     359               0 :         if (err!=TIFFReadDirEntryErrOk)
     360               0 :           return(err);
     361               0 :         err=TIFFReadDirEntryCheckRangeShortSlong8(m);
     362               0 :         if (err!=TIFFReadDirEntryErrOk)
     363               0 :           return(err);
     364               0 :         *value=(uint16)m;
     365               0 :         return(TIFFReadDirEntryErrOk);
     366                 :       }
     367                 :     default:
     368               0 :       return(TIFFReadDirEntryErrType);
     369                 :   }
     370                 : }
     371                 : 
     372           34902 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
     373                 : {
     374                 :   enum TIFFReadDirEntryErr err;
     375           34902 :   if (direntry->tdir_count!=1)
     376               0 :     return(TIFFReadDirEntryErrCount);
     377           34902 :   switch (direntry->tdir_type)
     378                 :   {
     379                 :     case TIFF_BYTE:
     380                 :       {
     381                 :         uint8 m;
     382               0 :         TIFFReadDirEntryCheckedByte(tif,direntry,&m);
     383               0 :         *value=(uint32)m;
     384               0 :         return(TIFFReadDirEntryErrOk);
     385                 :       }
     386                 :     case TIFF_SBYTE:
     387                 :       {
     388                 :         int8 m;
     389               0 :         TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
     390               0 :         err=TIFFReadDirEntryCheckRangeLongSbyte(m);
     391               0 :         if (err!=TIFFReadDirEntryErrOk)
     392               0 :           return(err);
     393               0 :         *value=(uint32)m;
     394               0 :         return(TIFFReadDirEntryErrOk);
     395                 :       }
     396                 :     case TIFF_SHORT:
     397                 :       {
     398                 :         uint16 m;
     399           32657 :         TIFFReadDirEntryCheckedShort(tif,direntry,&m);
     400           32657 :         *value=(uint32)m;
     401           32657 :         return(TIFFReadDirEntryErrOk);
     402                 :       }
     403                 :     case TIFF_SSHORT:
     404                 :       {
     405                 :         int16 m;
     406               0 :         TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
     407               0 :         err=TIFFReadDirEntryCheckRangeLongSshort(m);
     408               0 :         if (err!=TIFFReadDirEntryErrOk)
     409               0 :           return(err);
     410               0 :         *value=(uint32)m;
     411               0 :         return(TIFFReadDirEntryErrOk);
     412                 :       }
     413                 :     case TIFF_LONG:
     414            2245 :       TIFFReadDirEntryCheckedLong(tif,direntry,value);
     415            2245 :       return(TIFFReadDirEntryErrOk);
     416                 :     case TIFF_SLONG:
     417                 :       {
     418                 :         int32 m;
     419               0 :         TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
     420               0 :         err=TIFFReadDirEntryCheckRangeLongSlong(m);
     421               0 :         if (err!=TIFFReadDirEntryErrOk)
     422               0 :           return(err);
     423               0 :         *value=(uint32)m;
     424               0 :         return(TIFFReadDirEntryErrOk);
     425                 :       }
     426                 :     case TIFF_LONG8:
     427                 :       {
     428                 :         uint64 m;
     429               0 :         err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
     430               0 :         if (err!=TIFFReadDirEntryErrOk)
     431               0 :           return(err);
     432               0 :         err=TIFFReadDirEntryCheckRangeLongLong8(m);
     433               0 :         if (err!=TIFFReadDirEntryErrOk)
     434               0 :           return(err);
     435               0 :         *value=(uint32)m;
     436               0 :         return(TIFFReadDirEntryErrOk);
     437                 :       }
     438                 :     case TIFF_SLONG8:
     439                 :       {
     440                 :         int64 m;
     441               0 :         err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
     442               0 :         if (err!=TIFFReadDirEntryErrOk)
     443               0 :           return(err);
     444               0 :         err=TIFFReadDirEntryCheckRangeLongSlong8(m);
     445               0 :         if (err!=TIFFReadDirEntryErrOk)
     446               0 :           return(err);
     447               0 :         *value=(uint32)m;
     448               0 :         return(TIFFReadDirEntryErrOk);
     449                 :       }
     450                 :     default:
     451               0 :       return(TIFFReadDirEntryErrType);
     452                 :   }
     453                 : }
     454                 : 
     455               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
     456                 : {
     457                 :   enum TIFFReadDirEntryErr err;
     458               0 :   if (direntry->tdir_count!=1)
     459               0 :     return(TIFFReadDirEntryErrCount);
     460               0 :   switch (direntry->tdir_type)
     461                 :   {
     462                 :     case TIFF_BYTE:
     463                 :       {
     464                 :         uint8 m;
     465               0 :         TIFFReadDirEntryCheckedByte(tif,direntry,&m);
     466               0 :         *value=(uint64)m;
     467               0 :         return(TIFFReadDirEntryErrOk);
     468                 :       }
     469                 :     case TIFF_SBYTE:
     470                 :       {
     471                 :         int8 m;
     472               0 :         TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
     473               0 :         err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
     474               0 :         if (err!=TIFFReadDirEntryErrOk)
     475               0 :           return(err);
     476               0 :         *value=(uint64)m;
     477               0 :         return(TIFFReadDirEntryErrOk);
     478                 :       }
     479                 :     case TIFF_SHORT:
     480                 :       {
     481                 :         uint16 m;
     482               0 :         TIFFReadDirEntryCheckedShort(tif,direntry,&m);
     483               0 :         *value=(uint64)m;
     484               0 :         return(TIFFReadDirEntryErrOk);
     485                 :       }
     486                 :     case TIFF_SSHORT:
     487                 :       {
     488                 :         int16 m;
     489               0 :         TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
     490               0 :         err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
     491               0 :         if (err!=TIFFReadDirEntryErrOk)
     492               0 :           return(err);
     493               0 :         *value=(uint64)m;
     494               0 :         return(TIFFReadDirEntryErrOk);
     495                 :       }
     496                 :     case TIFF_LONG:
     497                 :       {
     498                 :         uint32 m;
     499               0 :         TIFFReadDirEntryCheckedLong(tif,direntry,&m);
     500               0 :         *value=(uint64)m;
     501               0 :         return(TIFFReadDirEntryErrOk);
     502                 :       }
     503                 :     case TIFF_SLONG:
     504                 :       {
     505                 :         int32 m;
     506               0 :         TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
     507               0 :         err=TIFFReadDirEntryCheckRangeLong8Slong(m);
     508               0 :         if (err!=TIFFReadDirEntryErrOk)
     509               0 :           return(err);
     510               0 :         *value=(uint64)m;
     511               0 :         return(TIFFReadDirEntryErrOk);
     512                 :       }
     513                 :     case TIFF_LONG8:
     514               0 :       err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
     515               0 :       return(err);
     516                 :     case TIFF_SLONG8:
     517                 :       {
     518                 :         int64 m;
     519               0 :         err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
     520               0 :         if (err!=TIFFReadDirEntryErrOk)
     521               0 :           return(err);
     522               0 :         err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
     523               0 :         if (err!=TIFFReadDirEntryErrOk)
     524               0 :           return(err);
     525               0 :         *value=(uint64)m;
     526               0 :         return(TIFFReadDirEntryErrOk);
     527                 :       }
     528                 :     default:
     529               0 :       return(TIFFReadDirEntryErrType);
     530                 :   }
     531                 : }
     532                 : 
     533               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
     534                 : {
     535                 :   enum TIFFReadDirEntryErr err;
     536               0 :   if (direntry->tdir_count!=1)
     537               0 :     return(TIFFReadDirEntryErrCount);
     538               0 :   switch (direntry->tdir_type)
     539                 :   {
     540                 :     case TIFF_BYTE:
     541                 :       {
     542                 :         uint8 m;
     543               0 :         TIFFReadDirEntryCheckedByte(tif,direntry,&m);
     544               0 :         *value=(float)m;
     545               0 :         return(TIFFReadDirEntryErrOk);
     546                 :       }
     547                 :     case TIFF_SBYTE:
     548                 :       {
     549                 :         int8 m;
     550               0 :         TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
     551               0 :         *value=(float)m;
     552               0 :         return(TIFFReadDirEntryErrOk);
     553                 :       }
     554                 :     case TIFF_SHORT:
     555                 :       {
     556                 :         uint16 m;
     557               0 :         TIFFReadDirEntryCheckedShort(tif,direntry,&m);
     558               0 :         *value=(float)m;
     559               0 :         return(TIFFReadDirEntryErrOk);
     560                 :       }
     561                 :     case TIFF_SSHORT:
     562                 :       {
     563                 :         int16 m;
     564               0 :         TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
     565               0 :         *value=(float)m;
     566               0 :         return(TIFFReadDirEntryErrOk);
     567                 :       }
     568                 :     case TIFF_LONG:
     569                 :       {
     570                 :         uint32 m;
     571               0 :         TIFFReadDirEntryCheckedLong(tif,direntry,&m);
     572               0 :         *value=(float)m;
     573               0 :         return(TIFFReadDirEntryErrOk);
     574                 :       }
     575                 :     case TIFF_SLONG:
     576                 :       {
     577                 :         int32 m;
     578               0 :         TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
     579               0 :         *value=(float)m;
     580               0 :         return(TIFFReadDirEntryErrOk);
     581                 :       }
     582                 :     case TIFF_LONG8:
     583                 :       {
     584                 :         uint64 m;
     585               0 :         err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
     586               0 :         if (err!=TIFFReadDirEntryErrOk)
     587               0 :           return(err);
     588                 : #if defined(__WIN32__) && (_MSC_VER < 1500)
     589                 :         /*
     590                 :          * XXX: MSVC 6.0 does not support conversion
     591                 :          * of 64-bit integers into floating point
     592                 :          * values.
     593                 :          */
     594                 :         *value = _TIFFUInt64ToFloat(m);
     595                 : #else
     596               0 :         *value=(float)m;
     597                 : #endif
     598               0 :         return(TIFFReadDirEntryErrOk);
     599                 :       }
     600                 :     case TIFF_SLONG8:
     601                 :       {
     602                 :         int64 m;
     603               0 :         err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
     604               0 :         if (err!=TIFFReadDirEntryErrOk)
     605               0 :           return(err);
     606               0 :         *value=(float)m;
     607               0 :         return(TIFFReadDirEntryErrOk);
     608                 :       }
     609                 :     case TIFF_RATIONAL:
     610                 :       {
     611                 :         double m;
     612               0 :         err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
     613               0 :         if (err!=TIFFReadDirEntryErrOk)
     614               0 :           return(err);
     615               0 :         *value=(float)m;
     616               0 :         return(TIFFReadDirEntryErrOk);
     617                 :       }
     618                 :     case TIFF_SRATIONAL:
     619                 :       {
     620                 :         double m;
     621               0 :         err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
     622               0 :         if (err!=TIFFReadDirEntryErrOk)
     623               0 :           return(err);
     624               0 :         *value=(float)m;
     625               0 :         return(TIFFReadDirEntryErrOk);
     626                 :       }
     627                 :     case TIFF_FLOAT:
     628               0 :       TIFFReadDirEntryCheckedFloat(tif,direntry,value);
     629               0 :       return(TIFFReadDirEntryErrOk);
     630                 :     case TIFF_DOUBLE:
     631                 :       {
     632                 :         double m;
     633               0 :         err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
     634               0 :         if (err!=TIFFReadDirEntryErrOk)
     635               0 :           return(err);
     636               0 :         *value=(float)m;
     637               0 :         return(TIFFReadDirEntryErrOk);
     638                 :       }
     639                 :     default:
     640               0 :       return(TIFFReadDirEntryErrType);
     641                 :   }
     642                 : }
     643                 : 
     644             130 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
     645                 : {
     646                 :   enum TIFFReadDirEntryErr err;
     647             130 :   if (direntry->tdir_count!=1)
     648               0 :     return(TIFFReadDirEntryErrCount);
     649             130 :   switch (direntry->tdir_type)
     650                 :   {
     651                 :     case TIFF_BYTE:
     652                 :       {
     653                 :         uint8 m;
     654               0 :         TIFFReadDirEntryCheckedByte(tif,direntry,&m);
     655               0 :         *value=(double)m;
     656               0 :         return(TIFFReadDirEntryErrOk);
     657                 :       }
     658                 :     case TIFF_SBYTE:
     659                 :       {
     660                 :         int8 m;
     661               0 :         TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
     662               0 :         *value=(double)m;
     663               0 :         return(TIFFReadDirEntryErrOk);
     664                 :       }
     665                 :     case TIFF_SHORT:
     666                 :       {
     667                 :         uint16 m;
     668               0 :         TIFFReadDirEntryCheckedShort(tif,direntry,&m);
     669               0 :         *value=(double)m;
     670               0 :         return(TIFFReadDirEntryErrOk);
     671                 :       }
     672                 :     case TIFF_SSHORT:
     673                 :       {
     674                 :         int16 m;
     675               0 :         TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
     676               0 :         *value=(double)m;
     677               0 :         return(TIFFReadDirEntryErrOk);
     678                 :       }
     679                 :     case TIFF_LONG:
     680                 :       {
     681                 :         uint32 m;
     682               0 :         TIFFReadDirEntryCheckedLong(tif,direntry,&m);
     683               0 :         *value=(double)m;
     684               0 :         return(TIFFReadDirEntryErrOk);
     685                 :       }
     686                 :     case TIFF_SLONG:
     687                 :       {
     688                 :         int32 m;
     689               0 :         TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
     690               0 :         *value=(double)m;
     691               0 :         return(TIFFReadDirEntryErrOk);
     692                 :       }
     693                 :     case TIFF_LONG8:
     694                 :       {
     695                 :         uint64 m;
     696               0 :         err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
     697               0 :         if (err!=TIFFReadDirEntryErrOk)
     698               0 :           return(err);
     699                 : #if defined(__WIN32__) && (_MSC_VER < 1500)
     700                 :         /*
     701                 :          * XXX: MSVC 6.0 does not support conversion
     702                 :          * of 64-bit integers into floating point
     703                 :          * values.
     704                 :          */
     705                 :         *value = _TIFFUInt64ToDouble(m);
     706                 : #else
     707               0 :         *value = (double)m;
     708                 : #endif
     709               0 :         return(TIFFReadDirEntryErrOk);
     710                 :       }
     711                 :     case TIFF_SLONG8:
     712                 :       {
     713                 :         int64 m;
     714               0 :         err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
     715               0 :         if (err!=TIFFReadDirEntryErrOk)
     716               0 :           return(err);
     717               0 :         *value=(double)m;
     718               0 :         return(TIFFReadDirEntryErrOk);
     719                 :       }
     720                 :     case TIFF_RATIONAL:
     721             130 :       err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
     722             130 :       return(err);
     723                 :     case TIFF_SRATIONAL:
     724               0 :       err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
     725               0 :       return(err);
     726                 :     case TIFF_FLOAT:
     727                 :       {
     728                 :         float m;
     729               0 :         TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
     730               0 :         *value=(double)m;
     731               0 :         return(TIFFReadDirEntryErrOk);
     732                 :       }
     733                 :     case TIFF_DOUBLE:
     734               0 :       err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
     735               0 :       return(err);
     736                 :     default:
     737               0 :       return(TIFFReadDirEntryErrType);
     738                 :   }
     739                 : }
     740                 : 
     741               9 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
     742                 : {
     743                 :   enum TIFFReadDirEntryErr err;
     744               9 :   if (direntry->tdir_count!=1)
     745               0 :     return(TIFFReadDirEntryErrCount);
     746               9 :   switch (direntry->tdir_type)
     747                 :   {
     748                 :     case TIFF_LONG:
     749                 :     case TIFF_IFD:
     750                 :       {
     751                 :         uint32 m;
     752               9 :         TIFFReadDirEntryCheckedLong(tif,direntry,&m);
     753               9 :         *value=(uint64)m;
     754               9 :         return(TIFFReadDirEntryErrOk);
     755                 :       }
     756                 :     case TIFF_LONG8:
     757                 :     case TIFF_IFD8:
     758               0 :       err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
     759               0 :       return(err);
     760                 :     default:
     761               0 :       return(TIFFReadDirEntryErrType);
     762                 :   }
     763                 : }
     764                 : 
     765           53736 : static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
     766                 : {
     767                 :   int typesize;
     768                 :   uint32 datasize;
     769                 :   void* data;
     770           53736 :   typesize=TIFFDataWidth(direntry->tdir_type);
     771           53736 :   if ((direntry->tdir_count==0)||(typesize==0))
     772                 :   {
     773               0 :     *value=0;
     774               0 :     return(TIFFReadDirEntryErrOk);
     775                 :   }
     776                 :         (void) desttypesize;
     777                 : 
     778                 :         /* 
     779                 :          * As a sanity check, make sure we have no more than a 2GB tag array 
     780                 :          * in either the current data type or the dest data type.  This also
     781                 :          * avoids problems with overflow of tmsize_t on 32bit systems.
     782                 :          */
     783           53736 :   if ((uint64)(2147483647/typesize)<direntry->tdir_count)
     784               0 :     return(TIFFReadDirEntryErrSizesan);
     785           53736 :   if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
     786               0 :     return(TIFFReadDirEntryErrSizesan);
     787                 : 
     788           53736 :   *count=(uint32)direntry->tdir_count;
     789           53736 :   datasize=(*count)*typesize;
     790           53736 :   assert((tmsize_t)datasize>0);
     791           53736 :   data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
     792           53736 :   if (data==0)
     793               0 :     return(TIFFReadDirEntryErrAlloc);
     794           53736 :   if (!(tif->tif_flags&TIFF_BIGTIFF))
     795                 :   {
     796           53652 :     if (datasize<=4)
     797           18307 :       _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
     798                 :     else
     799                 :     {
     800                 :       enum TIFFReadDirEntryErr err;
     801           35345 :       uint32 offset = direntry->tdir_offset.toff_long;
     802           35345 :       if (tif->tif_flags&TIFF_SWAB)
     803             827 :         TIFFSwabLong(&offset);
     804           35345 :       err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
     805           35345 :       if (err!=TIFFReadDirEntryErrOk)
     806                 :       {
     807               2 :         _TIFFfree(data);
     808               2 :         return(err);
     809                 :       }
     810                 :     }
     811                 :   }
     812                 :   else
     813                 :   {
     814              84 :     if (datasize<=8)
     815              82 :       _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
     816                 :     else
     817                 :     {
     818                 :       enum TIFFReadDirEntryErr err;
     819               2 :       uint64 offset = direntry->tdir_offset.toff_long8;
     820               2 :       if (tif->tif_flags&TIFF_SWAB)
     821               0 :         TIFFSwabLong8(&offset);
     822               2 :       err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
     823               2 :       if (err!=TIFFReadDirEntryErrOk)
     824                 :       {
     825               0 :         _TIFFfree(data);
     826               0 :         return(err);
     827                 :       }
     828                 :     }
     829                 :   }
     830           53734 :   *value=data;
     831           53734 :   return(TIFFReadDirEntryErrOk);
     832                 : }
     833                 : 
     834            7545 : static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
     835                 : {
     836                 :   enum TIFFReadDirEntryErr err;
     837                 :   uint32 count;
     838                 :   void* origdata;
     839                 :   uint8* data;
     840            7545 :   switch (direntry->tdir_type)
     841                 :   {
     842                 :     case TIFF_ASCII:
     843                 :     case TIFF_UNDEFINED:
     844                 :     case TIFF_BYTE:
     845                 :     case TIFF_SBYTE:
     846                 :     case TIFF_SHORT:
     847                 :     case TIFF_SSHORT:
     848                 :     case TIFF_LONG:
     849                 :     case TIFF_SLONG:
     850                 :     case TIFF_LONG8:
     851                 :     case TIFF_SLONG8:
     852                 :       break;
     853                 :     default:
     854               0 :       return(TIFFReadDirEntryErrType);
     855                 :   }
     856            7545 :   err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
     857           15088 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
     858                 :   {
     859               2 :     *value=0;
     860               2 :     return(err);
     861                 :   }
     862            7543 :   switch (direntry->tdir_type)
     863                 :   {
     864                 :     case TIFF_ASCII:
     865                 :     case TIFF_UNDEFINED:
     866                 :     case TIFF_BYTE:
     867            7543 :       *value=(uint8*)origdata;
     868            7543 :       return(TIFFReadDirEntryErrOk);
     869                 :     case TIFF_SBYTE:
     870                 :       {
     871                 :         int8* m;
     872                 :         uint32 n;
     873               0 :         m=(int8*)origdata;
     874               0 :         for (n=0; n<count; n++)
     875                 :         {
     876               0 :           err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
     877               0 :           if (err!=TIFFReadDirEntryErrOk)
     878                 :           {
     879               0 :             _TIFFfree(origdata);
     880               0 :             return(err);
     881                 :           }
     882               0 :           m++;
     883                 :         }
     884               0 :         *value=(uint8*)origdata;
     885               0 :         return(TIFFReadDirEntryErrOk);
     886                 :       }
     887                 :   }
     888               0 :   data=(uint8*)_TIFFmalloc(count);
     889               0 :   if (data==0)
     890                 :   {
     891               0 :     _TIFFfree(origdata);
     892               0 :     return(TIFFReadDirEntryErrAlloc);
     893                 :   }
     894               0 :   switch (direntry->tdir_type)
     895                 :   {
     896                 :     case TIFF_SHORT:
     897                 :       {
     898                 :         uint16* ma;
     899                 :         uint8* mb;
     900                 :         uint32 n;
     901               0 :         ma=(uint16*)origdata;
     902               0 :         mb=data;
     903               0 :         for (n=0; n<count; n++)
     904                 :         {
     905               0 :           if (tif->tif_flags&TIFF_SWAB)
     906               0 :             TIFFSwabShort(ma);
     907               0 :           err=TIFFReadDirEntryCheckRangeByteShort(*ma);
     908               0 :           if (err!=TIFFReadDirEntryErrOk)
     909               0 :             break;
     910               0 :           *mb++=(uint8)(*ma++);
     911                 :         }
     912                 :       }
     913               0 :       break;
     914                 :     case TIFF_SSHORT:
     915                 :       {
     916                 :         int16* ma;
     917                 :         uint8* mb;
     918                 :         uint32 n;
     919               0 :         ma=(int16*)origdata;
     920               0 :         mb=data;
     921               0 :         for (n=0; n<count; n++)
     922                 :         {
     923               0 :           if (tif->tif_flags&TIFF_SWAB)
     924               0 :             TIFFSwabShort((uint16*)ma);
     925               0 :           err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
     926               0 :           if (err!=TIFFReadDirEntryErrOk)
     927               0 :             break;
     928               0 :           *mb++=(uint8)(*ma++);
     929                 :         }
     930                 :       }
     931               0 :       break;
     932                 :     case TIFF_LONG:
     933                 :       {
     934                 :         uint32* ma;
     935                 :         uint8* mb;
     936                 :         uint32 n;
     937               0 :         ma=(uint32*)origdata;
     938               0 :         mb=data;
     939               0 :         for (n=0; n<count; n++)
     940                 :         {
     941               0 :           if (tif->tif_flags&TIFF_SWAB)
     942               0 :             TIFFSwabLong(ma);
     943               0 :           err=TIFFReadDirEntryCheckRangeByteLong(*ma);
     944               0 :           if (err!=TIFFReadDirEntryErrOk)
     945               0 :             break;
     946               0 :           *mb++=(uint8)(*ma++);
     947                 :         }
     948                 :       }
     949               0 :       break;
     950                 :     case TIFF_SLONG:
     951                 :       {
     952                 :         int32* ma;
     953                 :         uint8* mb;
     954                 :         uint32 n;
     955               0 :         ma=(int32*)origdata;
     956               0 :         mb=data;
     957               0 :         for (n=0; n<count; n++)
     958                 :         {
     959               0 :           if (tif->tif_flags&TIFF_SWAB)
     960               0 :             TIFFSwabLong((uint32*)ma);
     961               0 :           err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
     962               0 :           if (err!=TIFFReadDirEntryErrOk)
     963               0 :             break;
     964               0 :           *mb++=(uint8)(*ma++);
     965                 :         }
     966                 :       }
     967               0 :       break;
     968                 :     case TIFF_LONG8:
     969                 :       {
     970                 :         uint64* ma;
     971                 :         uint8* mb;
     972                 :         uint32 n;
     973               0 :         ma=(uint64*)origdata;
     974               0 :         mb=data;
     975               0 :         for (n=0; n<count; n++)
     976                 :         {
     977               0 :           if (tif->tif_flags&TIFF_SWAB)
     978               0 :             TIFFSwabLong8(ma);
     979               0 :           err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
     980               0 :           if (err!=TIFFReadDirEntryErrOk)
     981               0 :             break;
     982               0 :           *mb++=(uint8)(*ma++);
     983                 :         }
     984                 :       }
     985               0 :       break;
     986                 :     case TIFF_SLONG8:
     987                 :       {
     988                 :         int64* ma;
     989                 :         uint8* mb;
     990                 :         uint32 n;
     991               0 :         ma=(int64*)origdata;
     992               0 :         mb=data;
     993               0 :         for (n=0; n<count; n++)
     994                 :         {
     995               0 :           if (tif->tif_flags&TIFF_SWAB)
     996               0 :             TIFFSwabLong8((uint64*)ma);
     997               0 :           err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
     998               0 :           if (err!=TIFFReadDirEntryErrOk)
     999               0 :             break;
    1000               0 :           *mb++=(uint8)(*ma++);
    1001                 :         }
    1002                 :       }
    1003                 :       break;
    1004                 :   }
    1005               0 :   _TIFFfree(origdata);
    1006               0 :   if (err!=TIFFReadDirEntryErrOk)
    1007                 :   {
    1008               0 :     _TIFFfree(data);
    1009               0 :     return(err);
    1010                 :   }
    1011               0 :   *value=data;
    1012               0 :   return(TIFFReadDirEntryErrOk);
    1013                 : }
    1014                 : 
    1015               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
    1016                 : {
    1017                 :   enum TIFFReadDirEntryErr err;
    1018                 :   uint32 count;
    1019                 :   void* origdata;
    1020                 :   int8* data;
    1021               0 :   switch (direntry->tdir_type)
    1022                 :   {
    1023                 :     case TIFF_UNDEFINED:
    1024                 :     case TIFF_BYTE:
    1025                 :     case TIFF_SBYTE:
    1026                 :     case TIFF_SHORT:
    1027                 :     case TIFF_SSHORT:
    1028                 :     case TIFF_LONG:
    1029                 :     case TIFF_SLONG:
    1030                 :     case TIFF_LONG8:
    1031                 :     case TIFF_SLONG8:
    1032                 :       break;
    1033                 :     default:
    1034               0 :       return(TIFFReadDirEntryErrType);
    1035                 :   }
    1036               0 :   err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
    1037               0 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    1038                 :   {
    1039               0 :     *value=0;
    1040               0 :     return(err);
    1041                 :   }
    1042               0 :   switch (direntry->tdir_type)
    1043                 :   {
    1044                 :     case TIFF_UNDEFINED:
    1045                 :     case TIFF_BYTE:
    1046                 :       {
    1047                 :         uint8* m;
    1048                 :         uint32 n;
    1049               0 :         m=(uint8*)origdata;
    1050               0 :         for (n=0; n<count; n++)
    1051                 :         {
    1052               0 :           err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
    1053               0 :           if (err!=TIFFReadDirEntryErrOk)
    1054                 :           {
    1055               0 :             _TIFFfree(origdata);
    1056               0 :             return(err);
    1057                 :           }
    1058               0 :           m++;
    1059                 :         }
    1060               0 :         *value=(int8*)origdata;
    1061               0 :         return(TIFFReadDirEntryErrOk);
    1062                 :       }
    1063                 :     case TIFF_SBYTE:
    1064               0 :       *value=(int8*)origdata;
    1065               0 :       return(TIFFReadDirEntryErrOk);
    1066                 :   }
    1067               0 :   data=(int8*)_TIFFmalloc(count);
    1068               0 :   if (data==0)
    1069                 :   {
    1070               0 :     _TIFFfree(origdata);
    1071               0 :     return(TIFFReadDirEntryErrAlloc);
    1072                 :   }
    1073               0 :   switch (direntry->tdir_type)
    1074                 :   {
    1075                 :     case TIFF_SHORT:
    1076                 :       {
    1077                 :         uint16* ma;
    1078                 :         int8* mb;
    1079                 :         uint32 n;
    1080               0 :         ma=(uint16*)origdata;
    1081               0 :         mb=data;
    1082               0 :         for (n=0; n<count; n++)
    1083                 :         {
    1084               0 :           if (tif->tif_flags&TIFF_SWAB)
    1085               0 :             TIFFSwabShort(ma);
    1086               0 :           err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
    1087               0 :           if (err!=TIFFReadDirEntryErrOk)
    1088               0 :             break;
    1089               0 :           *mb++=(int8)(*ma++);
    1090                 :         }
    1091                 :       }
    1092               0 :       break;
    1093                 :     case TIFF_SSHORT:
    1094                 :       {
    1095                 :         int16* ma;
    1096                 :         int8* mb;
    1097                 :         uint32 n;
    1098               0 :         ma=(int16*)origdata;
    1099               0 :         mb=data;
    1100               0 :         for (n=0; n<count; n++)
    1101                 :         {
    1102               0 :           if (tif->tif_flags&TIFF_SWAB)
    1103               0 :             TIFFSwabShort((uint16*)ma);
    1104               0 :           err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
    1105               0 :           if (err!=TIFFReadDirEntryErrOk)
    1106               0 :             break;
    1107               0 :           *mb++=(int8)(*ma++);
    1108                 :         }
    1109                 :       }
    1110               0 :       break;
    1111                 :     case TIFF_LONG:
    1112                 :       {
    1113                 :         uint32* ma;
    1114                 :         int8* mb;
    1115                 :         uint32 n;
    1116               0 :         ma=(uint32*)origdata;
    1117               0 :         mb=data;
    1118               0 :         for (n=0; n<count; n++)
    1119                 :         {
    1120               0 :           if (tif->tif_flags&TIFF_SWAB)
    1121               0 :             TIFFSwabLong(ma);
    1122               0 :           err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
    1123               0 :           if (err!=TIFFReadDirEntryErrOk)
    1124               0 :             break;
    1125               0 :           *mb++=(int8)(*ma++);
    1126                 :         }
    1127                 :       }
    1128               0 :       break;
    1129                 :     case TIFF_SLONG:
    1130                 :       {
    1131                 :         int32* ma;
    1132                 :         int8* mb;
    1133                 :         uint32 n;
    1134               0 :         ma=(int32*)origdata;
    1135               0 :         mb=data;
    1136               0 :         for (n=0; n<count; n++)
    1137                 :         {
    1138               0 :           if (tif->tif_flags&TIFF_SWAB)
    1139               0 :             TIFFSwabLong((uint32*)ma);
    1140               0 :           err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
    1141               0 :           if (err!=TIFFReadDirEntryErrOk)
    1142               0 :             break;
    1143               0 :           *mb++=(int8)(*ma++);
    1144                 :         }
    1145                 :       }
    1146               0 :       break;
    1147                 :     case TIFF_LONG8:
    1148                 :       {
    1149                 :         uint64* ma;
    1150                 :         int8* mb;
    1151                 :         uint32 n;
    1152               0 :         ma=(uint64*)origdata;
    1153               0 :         mb=data;
    1154               0 :         for (n=0; n<count; n++)
    1155                 :         {
    1156               0 :           if (tif->tif_flags&TIFF_SWAB)
    1157               0 :             TIFFSwabLong8(ma);
    1158               0 :           err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
    1159               0 :           if (err!=TIFFReadDirEntryErrOk)
    1160               0 :             break;
    1161               0 :           *mb++=(int8)(*ma++);
    1162                 :         }
    1163                 :       }
    1164               0 :       break;
    1165                 :     case TIFF_SLONG8:
    1166                 :       {
    1167                 :         int64* ma;
    1168                 :         int8* mb;
    1169                 :         uint32 n;
    1170               0 :         ma=(int64*)origdata;
    1171               0 :         mb=data;
    1172               0 :         for (n=0; n<count; n++)
    1173                 :         {
    1174               0 :           if (tif->tif_flags&TIFF_SWAB)
    1175               0 :             TIFFSwabLong8((uint64*)ma);
    1176               0 :           err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
    1177               0 :           if (err!=TIFFReadDirEntryErrOk)
    1178               0 :             break;
    1179               0 :           *mb++=(int8)(*ma++);
    1180                 :         }
    1181                 :       }
    1182                 :       break;
    1183                 :   }
    1184               0 :   _TIFFfree(origdata);
    1185               0 :   if (err!=TIFFReadDirEntryErrOk)
    1186                 :   {
    1187               0 :     _TIFFfree(data);
    1188               0 :     return(err);
    1189                 :   }
    1190               0 :   *value=data;
    1191               0 :   return(TIFFReadDirEntryErrOk);
    1192                 : }
    1193                 : 
    1194           10905 : static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
    1195                 : {
    1196                 :   enum TIFFReadDirEntryErr err;
    1197                 :   uint32 count;
    1198                 :   void* origdata;
    1199                 :   uint16* data;
    1200           10905 :   switch (direntry->tdir_type)
    1201                 :   {
    1202                 :     case TIFF_BYTE:
    1203                 :     case TIFF_SBYTE:
    1204                 :     case TIFF_SHORT:
    1205                 :     case TIFF_SSHORT:
    1206                 :     case TIFF_LONG:
    1207                 :     case TIFF_SLONG:
    1208                 :     case TIFF_LONG8:
    1209                 :     case TIFF_SLONG8:
    1210                 :       break;
    1211                 :     default:
    1212               0 :       return(TIFFReadDirEntryErrType);
    1213                 :   }
    1214           10905 :   err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
    1215           21810 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    1216                 :   {
    1217               0 :     *value=0;
    1218               0 :     return(err);
    1219                 :   }
    1220           10905 :   switch (direntry->tdir_type)
    1221                 :   {
    1222                 :     case TIFF_SHORT:
    1223           10905 :       *value=(uint16*)origdata;
    1224           10905 :       if (tif->tif_flags&TIFF_SWAB)
    1225             522 :         TIFFSwabArrayOfShort(*value,count);  
    1226           10905 :       return(TIFFReadDirEntryErrOk);
    1227                 :     case TIFF_SSHORT:
    1228                 :       {
    1229                 :         int16* m;
    1230                 :         uint32 n;
    1231               0 :         m=(int16*)origdata;
    1232               0 :         for (n=0; n<count; n++)
    1233                 :         {
    1234               0 :           if (tif->tif_flags&TIFF_SWAB)
    1235               0 :             TIFFSwabShort((uint16*)m);
    1236               0 :           err=TIFFReadDirEntryCheckRangeShortSshort(*m);
    1237               0 :           if (err!=TIFFReadDirEntryErrOk)
    1238                 :           {
    1239               0 :             _TIFFfree(origdata);
    1240               0 :             return(err);
    1241                 :           }
    1242               0 :           m++;
    1243                 :         }
    1244               0 :         *value=(uint16*)origdata;
    1245               0 :         return(TIFFReadDirEntryErrOk);
    1246                 :       }
    1247                 :   }
    1248               0 :   data=(uint16*)_TIFFmalloc(count*2);
    1249               0 :   if (data==0)
    1250                 :   {
    1251               0 :     _TIFFfree(origdata);
    1252               0 :     return(TIFFReadDirEntryErrAlloc);
    1253                 :   }
    1254               0 :   switch (direntry->tdir_type)
    1255                 :   {
    1256                 :     case TIFF_BYTE:
    1257                 :       {
    1258                 :         uint8* ma;
    1259                 :         uint16* mb;
    1260                 :         uint32 n;
    1261               0 :         ma=(uint8*)origdata;
    1262               0 :         mb=data;
    1263               0 :         for (n=0; n<count; n++)
    1264               0 :           *mb++=(uint16)(*ma++);
    1265                 :       }
    1266               0 :       break;
    1267                 :     case TIFF_SBYTE:
    1268                 :       {
    1269                 :         int8* ma;
    1270                 :         uint16* mb;
    1271                 :         uint32 n;
    1272               0 :         ma=(int8*)origdata;
    1273               0 :         mb=data;
    1274               0 :         for (n=0; n<count; n++)
    1275                 :         {
    1276               0 :           err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
    1277               0 :           if (err!=TIFFReadDirEntryErrOk)
    1278               0 :             break;
    1279               0 :           *mb++=(uint16)(*ma++);
    1280                 :         }
    1281                 :       }
    1282               0 :       break;
    1283                 :     case TIFF_LONG:
    1284                 :       {
    1285                 :         uint32* ma;
    1286                 :         uint16* mb;
    1287                 :         uint32 n;
    1288               0 :         ma=(uint32*)origdata;
    1289               0 :         mb=data;
    1290               0 :         for (n=0; n<count; n++)
    1291                 :         {
    1292               0 :           if (tif->tif_flags&TIFF_SWAB)
    1293               0 :             TIFFSwabLong(ma);
    1294               0 :           err=TIFFReadDirEntryCheckRangeShortLong(*ma);
    1295               0 :           if (err!=TIFFReadDirEntryErrOk)
    1296               0 :             break;
    1297               0 :           *mb++=(uint16)(*ma++);
    1298                 :         }
    1299                 :       }
    1300               0 :       break;
    1301                 :     case TIFF_SLONG:
    1302                 :       {
    1303                 :         int32* ma;
    1304                 :         uint16* mb;
    1305                 :         uint32 n;
    1306               0 :         ma=(int32*)origdata;
    1307               0 :         mb=data;
    1308               0 :         for (n=0; n<count; n++)
    1309                 :         {
    1310               0 :           if (tif->tif_flags&TIFF_SWAB)
    1311               0 :             TIFFSwabLong((uint32*)ma);
    1312               0 :           err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
    1313               0 :           if (err!=TIFFReadDirEntryErrOk)
    1314               0 :             break;
    1315               0 :           *mb++=(uint16)(*ma++);
    1316                 :         }
    1317                 :       }
    1318               0 :       break;
    1319                 :     case TIFF_LONG8:
    1320                 :       {
    1321                 :         uint64* ma;
    1322                 :         uint16* mb;
    1323                 :         uint32 n;
    1324               0 :         ma=(uint64*)origdata;
    1325               0 :         mb=data;
    1326               0 :         for (n=0; n<count; n++)
    1327                 :         {
    1328               0 :           if (tif->tif_flags&TIFF_SWAB)
    1329               0 :             TIFFSwabLong8(ma);
    1330               0 :           err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
    1331               0 :           if (err!=TIFFReadDirEntryErrOk)
    1332               0 :             break;
    1333               0 :           *mb++=(uint16)(*ma++);
    1334                 :         }
    1335                 :       }
    1336               0 :       break;
    1337                 :     case TIFF_SLONG8:
    1338                 :       {
    1339                 :         int64* ma;
    1340                 :         uint16* mb;
    1341                 :         uint32 n;
    1342               0 :         ma=(int64*)origdata;
    1343               0 :         mb=data;
    1344               0 :         for (n=0; n<count; n++)
    1345                 :         {
    1346               0 :           if (tif->tif_flags&TIFF_SWAB)
    1347               0 :             TIFFSwabLong8((uint64*)ma);
    1348               0 :           err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
    1349               0 :           if (err!=TIFFReadDirEntryErrOk)
    1350               0 :             break;
    1351               0 :           *mb++=(uint16)(*ma++);
    1352                 :         }
    1353                 :       }
    1354                 :       break;
    1355                 :   }
    1356               0 :   _TIFFfree(origdata);
    1357               0 :   if (err!=TIFFReadDirEntryErrOk)
    1358                 :   {
    1359               0 :     _TIFFfree(data);
    1360               0 :     return(err);
    1361                 :   }
    1362               0 :   *value=data;
    1363               0 :   return(TIFFReadDirEntryErrOk);
    1364                 : }
    1365                 : 
    1366               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
    1367                 : {
    1368                 :   enum TIFFReadDirEntryErr err;
    1369                 :   uint32 count;
    1370                 :   void* origdata;
    1371                 :   int16* data;
    1372               0 :   switch (direntry->tdir_type)
    1373                 :   {
    1374                 :     case TIFF_BYTE:
    1375                 :     case TIFF_SBYTE:
    1376                 :     case TIFF_SHORT:
    1377                 :     case TIFF_SSHORT:
    1378                 :     case TIFF_LONG:
    1379                 :     case TIFF_SLONG:
    1380                 :     case TIFF_LONG8:
    1381                 :     case TIFF_SLONG8:
    1382                 :       break;
    1383                 :     default:
    1384               0 :       return(TIFFReadDirEntryErrType);
    1385                 :   }
    1386               0 :   err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
    1387               0 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    1388                 :   {
    1389               0 :     *value=0;
    1390               0 :     return(err);
    1391                 :   }
    1392               0 :   switch (direntry->tdir_type)
    1393                 :   {
    1394                 :     case TIFF_SHORT:
    1395                 :       {
    1396                 :         uint16* m;
    1397                 :         uint32 n;
    1398               0 :         m=(uint16*)origdata;
    1399               0 :         for (n=0; n<count; n++)
    1400                 :         {
    1401               0 :           if (tif->tif_flags&TIFF_SWAB)
    1402               0 :             TIFFSwabShort(m);
    1403               0 :           err=TIFFReadDirEntryCheckRangeSshortShort(*m);
    1404               0 :           if (err!=TIFFReadDirEntryErrOk)
    1405                 :           {
    1406               0 :             _TIFFfree(origdata);
    1407               0 :             return(err);
    1408                 :           }
    1409               0 :           m++;
    1410                 :         }
    1411               0 :         *value=(int16*)origdata;
    1412               0 :         return(TIFFReadDirEntryErrOk);
    1413                 :       }
    1414                 :     case TIFF_SSHORT:
    1415               0 :       *value=(int16*)origdata;
    1416               0 :       if (tif->tif_flags&TIFF_SWAB)
    1417               0 :         TIFFSwabArrayOfShort((uint16*)(*value),count);
    1418               0 :       return(TIFFReadDirEntryErrOk);
    1419                 :   }
    1420               0 :   data=(int16*)_TIFFmalloc(count*2);
    1421               0 :   if (data==0)
    1422                 :   {
    1423               0 :     _TIFFfree(origdata);
    1424               0 :     return(TIFFReadDirEntryErrAlloc);
    1425                 :   }
    1426               0 :   switch (direntry->tdir_type)
    1427                 :   {
    1428                 :     case TIFF_BYTE:
    1429                 :       {
    1430                 :         uint8* ma;
    1431                 :         int16* mb;
    1432                 :         uint32 n;
    1433               0 :         ma=(uint8*)origdata;
    1434               0 :         mb=data;
    1435               0 :         for (n=0; n<count; n++)
    1436               0 :           *mb++=(int16)(*ma++);
    1437                 :       }
    1438               0 :       break;
    1439                 :     case TIFF_SBYTE:
    1440                 :       {
    1441                 :         int8* ma;
    1442                 :         int16* mb;
    1443                 :         uint32 n;
    1444               0 :         ma=(int8*)origdata;
    1445               0 :         mb=data;
    1446               0 :         for (n=0; n<count; n++)
    1447               0 :           *mb++=(int16)(*ma++);
    1448                 :       }
    1449               0 :       break;
    1450                 :     case TIFF_LONG:
    1451                 :       {
    1452                 :         uint32* ma;
    1453                 :         int16* mb;
    1454                 :         uint32 n;
    1455               0 :         ma=(uint32*)origdata;
    1456               0 :         mb=data;
    1457               0 :         for (n=0; n<count; n++)
    1458                 :         {
    1459               0 :           if (tif->tif_flags&TIFF_SWAB)
    1460               0 :             TIFFSwabLong(ma);
    1461               0 :           err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
    1462               0 :           if (err!=TIFFReadDirEntryErrOk)
    1463               0 :             break;
    1464               0 :           *mb++=(int16)(*ma++);
    1465                 :         }
    1466                 :       }
    1467               0 :       break;
    1468                 :     case TIFF_SLONG:
    1469                 :       {
    1470                 :         int32* ma;
    1471                 :         int16* mb;
    1472                 :         uint32 n;
    1473               0 :         ma=(int32*)origdata;
    1474               0 :         mb=data;
    1475               0 :         for (n=0; n<count; n++)
    1476                 :         {
    1477               0 :           if (tif->tif_flags&TIFF_SWAB)
    1478               0 :             TIFFSwabLong((uint32*)ma);
    1479               0 :           err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
    1480               0 :           if (err!=TIFFReadDirEntryErrOk)
    1481               0 :             break;
    1482               0 :           *mb++=(int16)(*ma++);
    1483                 :         }
    1484                 :       }
    1485               0 :       break;
    1486                 :     case TIFF_LONG8:
    1487                 :       {
    1488                 :         uint64* ma;
    1489                 :         int16* mb;
    1490                 :         uint32 n;
    1491               0 :         ma=(uint64*)origdata;
    1492               0 :         mb=data;
    1493               0 :         for (n=0; n<count; n++)
    1494                 :         {
    1495               0 :           if (tif->tif_flags&TIFF_SWAB)
    1496               0 :             TIFFSwabLong8(ma);
    1497               0 :           err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
    1498               0 :           if (err!=TIFFReadDirEntryErrOk)
    1499               0 :             break;
    1500               0 :           *mb++=(int16)(*ma++);
    1501                 :         }
    1502                 :       }
    1503               0 :       break;
    1504                 :     case TIFF_SLONG8:
    1505                 :       {
    1506                 :         int64* ma;
    1507                 :         int16* mb;
    1508                 :         uint32 n;
    1509               0 :         ma=(int64*)origdata;
    1510               0 :         mb=data;
    1511               0 :         for (n=0; n<count; n++)
    1512                 :         {
    1513               0 :           if (tif->tif_flags&TIFF_SWAB)
    1514               0 :             TIFFSwabLong8((uint64*)ma);
    1515               0 :           err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
    1516               0 :           if (err!=TIFFReadDirEntryErrOk)
    1517               0 :             break;
    1518               0 :           *mb++=(int16)(*ma++);
    1519                 :         }
    1520                 :       }
    1521                 :       break;
    1522                 :   }
    1523               0 :   _TIFFfree(origdata);
    1524               0 :   if (err!=TIFFReadDirEntryErrOk)
    1525                 :   {
    1526               0 :     _TIFFfree(data);
    1527               0 :     return(err);
    1528                 :   }
    1529               0 :   *value=data;
    1530               0 :   return(TIFFReadDirEntryErrOk);
    1531                 : }
    1532                 : 
    1533               1 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
    1534                 : {
    1535                 :   enum TIFFReadDirEntryErr err;
    1536                 :   uint32 count;
    1537                 :   void* origdata;
    1538                 :   uint32* data;
    1539               1 :   switch (direntry->tdir_type)
    1540                 :   {
    1541                 :     case TIFF_BYTE:
    1542                 :     case TIFF_SBYTE:
    1543                 :     case TIFF_SHORT:
    1544                 :     case TIFF_SSHORT:
    1545                 :     case TIFF_LONG:
    1546                 :     case TIFF_SLONG:
    1547                 :     case TIFF_LONG8:
    1548                 :     case TIFF_SLONG8:
    1549                 :       break;
    1550                 :     default:
    1551               0 :       return(TIFFReadDirEntryErrType);
    1552                 :   }
    1553               1 :   err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
    1554               2 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    1555                 :   {
    1556               0 :     *value=0;
    1557               0 :     return(err);
    1558                 :   }
    1559               1 :   switch (direntry->tdir_type)
    1560                 :   {
    1561                 :     case TIFF_LONG:
    1562               1 :       *value=(uint32*)origdata;
    1563               1 :       if (tif->tif_flags&TIFF_SWAB)
    1564               0 :         TIFFSwabArrayOfLong(*value,count);
    1565               1 :       return(TIFFReadDirEntryErrOk);
    1566                 :     case TIFF_SLONG:
    1567                 :       {
    1568                 :         int32* m;
    1569                 :         uint32 n;
    1570               0 :         m=(int32*)origdata;
    1571               0 :         for (n=0; n<count; n++)
    1572                 :         {
    1573               0 :           if (tif->tif_flags&TIFF_SWAB)
    1574               0 :             TIFFSwabLong((uint32*)m);
    1575               0 :           err=TIFFReadDirEntryCheckRangeLongSlong(*m);
    1576               0 :           if (err!=TIFFReadDirEntryErrOk)
    1577                 :           {
    1578               0 :             _TIFFfree(origdata);
    1579               0 :             return(err);
    1580                 :           }
    1581               0 :           m++;
    1582                 :         }
    1583               0 :         *value=(uint32*)origdata;
    1584               0 :         return(TIFFReadDirEntryErrOk);
    1585                 :       }
    1586                 :   }
    1587               0 :   data=(uint32*)_TIFFmalloc(count*4);
    1588               0 :   if (data==0)
    1589                 :   {
    1590               0 :     _TIFFfree(origdata);
    1591               0 :     return(TIFFReadDirEntryErrAlloc);
    1592                 :   }
    1593               0 :   switch (direntry->tdir_type)
    1594                 :   {
    1595                 :     case TIFF_BYTE:
    1596                 :       {
    1597                 :         uint8* ma;
    1598                 :         uint32* mb;
    1599                 :         uint32 n;
    1600               0 :         ma=(uint8*)origdata;
    1601               0 :         mb=data;
    1602               0 :         for (n=0; n<count; n++)
    1603               0 :           *mb++=(uint32)(*ma++);
    1604                 :       }
    1605               0 :       break;
    1606                 :     case TIFF_SBYTE:
    1607                 :       {
    1608                 :         int8* ma;
    1609                 :         uint32* mb;
    1610                 :         uint32 n;
    1611               0 :         ma=(int8*)origdata;
    1612               0 :         mb=data;
    1613               0 :         for (n=0; n<count; n++)
    1614                 :         {
    1615               0 :           err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
    1616               0 :           if (err!=TIFFReadDirEntryErrOk)
    1617               0 :             break;
    1618               0 :           *mb++=(uint32)(*ma++);
    1619                 :         }
    1620                 :       }
    1621               0 :       break;
    1622                 :     case TIFF_SHORT:
    1623                 :       {
    1624                 :         uint16* ma;
    1625                 :         uint32* mb;
    1626                 :         uint32 n;
    1627               0 :         ma=(uint16*)origdata;
    1628               0 :         mb=data;
    1629               0 :         for (n=0; n<count; n++)
    1630                 :         {
    1631               0 :           if (tif->tif_flags&TIFF_SWAB)
    1632               0 :             TIFFSwabShort(ma);
    1633               0 :           *mb++=(uint32)(*ma++);
    1634                 :         }
    1635                 :       }
    1636               0 :       break;
    1637                 :     case TIFF_SSHORT:
    1638                 :       {
    1639                 :         int16* ma;
    1640                 :         uint32* mb;
    1641                 :         uint32 n;
    1642               0 :         ma=(int16*)origdata;
    1643               0 :         mb=data;
    1644               0 :         for (n=0; n<count; n++)
    1645                 :         {
    1646               0 :           if (tif->tif_flags&TIFF_SWAB)
    1647               0 :             TIFFSwabShort((uint16*)ma);
    1648               0 :           err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
    1649               0 :           if (err!=TIFFReadDirEntryErrOk)
    1650               0 :             break;
    1651               0 :           *mb++=(uint32)(*ma++);
    1652                 :         }
    1653                 :       }
    1654               0 :       break;
    1655                 :     case TIFF_LONG8:
    1656                 :       {
    1657                 :         uint64* ma;
    1658                 :         uint32* mb;
    1659                 :         uint32 n;
    1660               0 :         ma=(uint64*)origdata;
    1661               0 :         mb=data;
    1662               0 :         for (n=0; n<count; n++)
    1663                 :         {
    1664               0 :           if (tif->tif_flags&TIFF_SWAB)
    1665               0 :             TIFFSwabLong8(ma);
    1666               0 :           err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
    1667               0 :           if (err!=TIFFReadDirEntryErrOk)
    1668               0 :             break;
    1669               0 :           *mb++=(uint32)(*ma++);
    1670                 :         }
    1671                 :       }
    1672               0 :       break;
    1673                 :     case TIFF_SLONG8:
    1674                 :       {
    1675                 :         int64* ma;
    1676                 :         uint32* mb;
    1677                 :         uint32 n;
    1678               0 :         ma=(int64*)origdata;
    1679               0 :         mb=data;
    1680               0 :         for (n=0; n<count; n++)
    1681                 :         {
    1682               0 :           if (tif->tif_flags&TIFF_SWAB)
    1683               0 :             TIFFSwabLong8((uint64*)ma);
    1684               0 :           err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
    1685               0 :           if (err!=TIFFReadDirEntryErrOk)
    1686               0 :             break;
    1687               0 :           *mb++=(uint32)(*ma++);
    1688                 :         }
    1689                 :       }
    1690                 :       break;
    1691                 :   }
    1692               0 :   _TIFFfree(origdata);
    1693               0 :   if (err!=TIFFReadDirEntryErrOk)
    1694                 :   {
    1695               0 :     _TIFFfree(data);
    1696               0 :     return(err);
    1697                 :   }
    1698               0 :   *value=data;
    1699               0 :   return(TIFFReadDirEntryErrOk);
    1700                 : }
    1701                 : 
    1702               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
    1703                 : {
    1704                 :   enum TIFFReadDirEntryErr err;
    1705                 :   uint32 count;
    1706                 :   void* origdata;
    1707                 :   int32* data;
    1708               0 :   switch (direntry->tdir_type)
    1709                 :   {
    1710                 :     case TIFF_BYTE:
    1711                 :     case TIFF_SBYTE:
    1712                 :     case TIFF_SHORT:
    1713                 :     case TIFF_SSHORT:
    1714                 :     case TIFF_LONG:
    1715                 :     case TIFF_SLONG:
    1716                 :     case TIFF_LONG8:
    1717                 :     case TIFF_SLONG8:
    1718                 :       break;
    1719                 :     default:
    1720               0 :       return(TIFFReadDirEntryErrType);
    1721                 :   }
    1722               0 :   err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
    1723               0 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    1724                 :   {
    1725               0 :     *value=0;
    1726               0 :     return(err);
    1727                 :   }
    1728               0 :   switch (direntry->tdir_type)
    1729                 :   {
    1730                 :     case TIFF_LONG:
    1731                 :       {
    1732                 :         uint32* m;
    1733                 :         uint32 n;
    1734               0 :         m=(uint32*)origdata;
    1735               0 :         for (n=0; n<count; n++)
    1736                 :         {
    1737               0 :           if (tif->tif_flags&TIFF_SWAB)
    1738               0 :             TIFFSwabLong((uint32*)m);
    1739               0 :           err=TIFFReadDirEntryCheckRangeSlongLong(*m);
    1740               0 :           if (err!=TIFFReadDirEntryErrOk)
    1741                 :           {
    1742               0 :             _TIFFfree(origdata);
    1743               0 :             return(err);
    1744                 :           }
    1745               0 :           m++;
    1746                 :         }
    1747               0 :         *value=(int32*)origdata;
    1748               0 :         return(TIFFReadDirEntryErrOk);
    1749                 :       }
    1750                 :     case TIFF_SLONG:
    1751               0 :       *value=(int32*)origdata;
    1752               0 :       if (tif->tif_flags&TIFF_SWAB)
    1753               0 :         TIFFSwabArrayOfLong((uint32*)(*value),count);
    1754               0 :       return(TIFFReadDirEntryErrOk);
    1755                 :   }
    1756               0 :   data=(int32*)_TIFFmalloc(count*4);
    1757               0 :   if (data==0)
    1758                 :   {
    1759               0 :     _TIFFfree(origdata);
    1760               0 :     return(TIFFReadDirEntryErrAlloc);
    1761                 :   }
    1762               0 :   switch (direntry->tdir_type)
    1763                 :   {
    1764                 :     case TIFF_BYTE:
    1765                 :       {
    1766                 :         uint8* ma;
    1767                 :         int32* mb;
    1768                 :         uint32 n;
    1769               0 :         ma=(uint8*)origdata;
    1770               0 :         mb=data;
    1771               0 :         for (n=0; n<count; n++)
    1772               0 :           *mb++=(int32)(*ma++);
    1773                 :       }
    1774               0 :       break;
    1775                 :     case TIFF_SBYTE:
    1776                 :       {
    1777                 :         int8* ma;
    1778                 :         int32* mb;
    1779                 :         uint32 n;
    1780               0 :         ma=(int8*)origdata;
    1781               0 :         mb=data;
    1782               0 :         for (n=0; n<count; n++)
    1783               0 :           *mb++=(int32)(*ma++);
    1784                 :       }
    1785               0 :       break;
    1786                 :     case TIFF_SHORT:
    1787                 :       {
    1788                 :         uint16* ma;
    1789                 :         int32* mb;
    1790                 :         uint32 n;
    1791               0 :         ma=(uint16*)origdata;
    1792               0 :         mb=data;
    1793               0 :         for (n=0; n<count; n++)
    1794                 :         {
    1795               0 :           if (tif->tif_flags&TIFF_SWAB)
    1796               0 :             TIFFSwabShort(ma);
    1797               0 :           *mb++=(int32)(*ma++);
    1798                 :         }
    1799                 :       }
    1800               0 :       break;
    1801                 :     case TIFF_SSHORT:
    1802                 :       {
    1803                 :         int16* ma;
    1804                 :         int32* mb;
    1805                 :         uint32 n;
    1806               0 :         ma=(int16*)origdata;
    1807               0 :         mb=data;
    1808               0 :         for (n=0; n<count; n++)
    1809                 :         {
    1810               0 :           if (tif->tif_flags&TIFF_SWAB)
    1811               0 :             TIFFSwabShort((uint16*)ma);
    1812               0 :           *mb++=(int32)(*ma++);
    1813                 :         }
    1814                 :       }
    1815               0 :       break;
    1816                 :     case TIFF_LONG8:
    1817                 :       {
    1818                 :         uint64* ma;
    1819                 :         int32* mb;
    1820                 :         uint32 n;
    1821               0 :         ma=(uint64*)origdata;
    1822               0 :         mb=data;
    1823               0 :         for (n=0; n<count; n++)
    1824                 :         {
    1825               0 :           if (tif->tif_flags&TIFF_SWAB)
    1826               0 :             TIFFSwabLong8(ma);
    1827               0 :           err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
    1828               0 :           if (err!=TIFFReadDirEntryErrOk)
    1829               0 :             break;
    1830               0 :           *mb++=(int32)(*ma++);
    1831                 :         }
    1832                 :       }
    1833               0 :       break;
    1834                 :     case TIFF_SLONG8:
    1835                 :       {
    1836                 :         int64* ma;
    1837                 :         int32* mb;
    1838                 :         uint32 n;
    1839               0 :         ma=(int64*)origdata;
    1840               0 :         mb=data;
    1841               0 :         for (n=0; n<count; n++)
    1842                 :         {
    1843               0 :           if (tif->tif_flags&TIFF_SWAB)
    1844               0 :             TIFFSwabLong8((uint64*)ma);
    1845               0 :           err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
    1846               0 :           if (err!=TIFFReadDirEntryErrOk)
    1847               0 :             break;
    1848               0 :           *mb++=(int32)(*ma++);
    1849                 :         }
    1850                 :       }
    1851                 :       break;
    1852                 :   }
    1853               0 :   _TIFFfree(origdata);
    1854               0 :   if (err!=TIFFReadDirEntryErrOk)
    1855                 :   {
    1856               0 :     _TIFFfree(data);
    1857               0 :     return(err);
    1858                 :   }
    1859               0 :   *value=data;
    1860               0 :   return(TIFFReadDirEntryErrOk);
    1861                 : }
    1862                 : 
    1863           19145 : static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
    1864                 : {
    1865                 :   enum TIFFReadDirEntryErr err;
    1866                 :   uint32 count;
    1867                 :   void* origdata;
    1868                 :   uint64* data;
    1869           19145 :   switch (direntry->tdir_type)
    1870                 :   {
    1871                 :     case TIFF_BYTE:
    1872                 :     case TIFF_SBYTE:
    1873                 :     case TIFF_SHORT:
    1874                 :     case TIFF_SSHORT:
    1875                 :     case TIFF_LONG:
    1876                 :     case TIFF_SLONG:
    1877                 :     case TIFF_LONG8:
    1878                 :     case TIFF_SLONG8:
    1879                 :       break;
    1880                 :     default:
    1881               0 :       return(TIFFReadDirEntryErrType);
    1882                 :   }
    1883           19145 :   err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
    1884           38290 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    1885                 :   {
    1886               0 :     *value=0;
    1887               0 :     return(err);
    1888                 :   }
    1889           19145 :   switch (direntry->tdir_type)
    1890                 :   {
    1891                 :     case TIFF_LONG8:
    1892              84 :       *value=(uint64*)origdata;
    1893              84 :       if (tif->tif_flags&TIFF_SWAB)
    1894               0 :         TIFFSwabArrayOfLong8(*value,count);
    1895              84 :       return(TIFFReadDirEntryErrOk);
    1896                 :     case TIFF_SLONG8:
    1897                 :       {
    1898                 :         int64* m;
    1899                 :         uint32 n;
    1900               0 :         m=(int64*)origdata;
    1901               0 :         for (n=0; n<count; n++)
    1902                 :         {
    1903               0 :           if (tif->tif_flags&TIFF_SWAB)
    1904               0 :             TIFFSwabLong8((uint64*)m);
    1905               0 :           err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
    1906               0 :           if (err!=TIFFReadDirEntryErrOk)
    1907                 :           {
    1908               0 :             _TIFFfree(origdata);
    1909               0 :             return(err);
    1910                 :           }
    1911               0 :           m++;
    1912                 :         }
    1913               0 :         *value=(uint64*)origdata;
    1914               0 :         return(TIFFReadDirEntryErrOk);
    1915                 :       }
    1916                 :   }
    1917           19061 :   data=(uint64*)_TIFFmalloc(count*8);
    1918           19061 :   if (data==0)
    1919                 :   {
    1920               0 :     _TIFFfree(origdata);
    1921               0 :     return(TIFFReadDirEntryErrAlloc);
    1922                 :   }
    1923           19061 :   switch (direntry->tdir_type)
    1924                 :   {
    1925                 :     case TIFF_BYTE:
    1926                 :       {
    1927                 :         uint8* ma;
    1928                 :         uint64* mb;
    1929                 :         uint32 n;
    1930               0 :         ma=(uint8*)origdata;
    1931               0 :         mb=data;
    1932               0 :         for (n=0; n<count; n++)
    1933               0 :           *mb++=(uint64)(*ma++);
    1934                 :       }
    1935               0 :       break;
    1936                 :     case TIFF_SBYTE:
    1937                 :       {
    1938                 :         int8* ma;
    1939                 :         uint64* mb;
    1940                 :         uint32 n;
    1941               0 :         ma=(int8*)origdata;
    1942               0 :         mb=data;
    1943               0 :         for (n=0; n<count; n++)
    1944                 :         {
    1945               0 :           err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
    1946               0 :           if (err!=TIFFReadDirEntryErrOk)
    1947               0 :             break;
    1948               0 :           *mb++=(uint64)(*ma++);
    1949                 :         }
    1950                 :       }
    1951               0 :       break;
    1952                 :     case TIFF_SHORT:
    1953                 :       {
    1954                 :         uint16* ma;
    1955                 :         uint64* mb;
    1956                 :         uint32 n;
    1957               0 :         ma=(uint16*)origdata;
    1958               0 :         mb=data;
    1959               0 :         for (n=0; n<count; n++)
    1960                 :         {
    1961               0 :           if (tif->tif_flags&TIFF_SWAB)
    1962               0 :             TIFFSwabShort(ma);
    1963               0 :           *mb++=(uint64)(*ma++);
    1964                 :         }
    1965                 :       }
    1966               0 :       break;
    1967                 :     case TIFF_SSHORT:
    1968                 :       {
    1969                 :         int16* ma;
    1970                 :         uint64* mb;
    1971                 :         uint32 n;
    1972               0 :         ma=(int16*)origdata;
    1973               0 :         mb=data;
    1974               0 :         for (n=0; n<count; n++)
    1975                 :         {
    1976               0 :           if (tif->tif_flags&TIFF_SWAB)
    1977               0 :             TIFFSwabShort((uint16*)ma);
    1978               0 :           err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
    1979               0 :           if (err!=TIFFReadDirEntryErrOk)
    1980               0 :             break;
    1981               0 :           *mb++=(uint64)(*ma++);
    1982                 :         }
    1983                 :       }
    1984               0 :       break;
    1985                 :     case TIFF_LONG:
    1986                 :       {
    1987                 :         uint32* ma;
    1988                 :         uint64* mb;
    1989                 :         uint32 n;
    1990           19061 :         ma=(uint32*)origdata;
    1991           19061 :         mb=data;
    1992          528032 :         for (n=0; n<count; n++)
    1993                 :         {
    1994          508971 :           if (tif->tif_flags&TIFF_SWAB)
    1995            1369 :             TIFFSwabLong(ma);
    1996          508971 :           *mb++=(uint64)(*ma++);
    1997                 :         }
    1998                 :       }
    1999           19061 :       break;
    2000                 :     case TIFF_SLONG:
    2001                 :       {
    2002                 :         int32* ma;
    2003                 :         uint64* mb;
    2004                 :         uint32 n;
    2005               0 :         ma=(int32*)origdata;
    2006               0 :         mb=data;
    2007               0 :         for (n=0; n<count; n++)
    2008                 :         {
    2009               0 :           if (tif->tif_flags&TIFF_SWAB)
    2010               0 :             TIFFSwabLong((uint32*)ma);
    2011               0 :           err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
    2012               0 :           if (err!=TIFFReadDirEntryErrOk)
    2013               0 :             break;
    2014               0 :           *mb++=(uint64)(*ma++);
    2015                 :         }
    2016                 :       }
    2017                 :       break;
    2018                 :   }
    2019           19061 :   _TIFFfree(origdata);
    2020           19061 :   if (err!=TIFFReadDirEntryErrOk)
    2021                 :   {
    2022               0 :     _TIFFfree(data);
    2023               0 :     return(err);
    2024                 :   }
    2025           19061 :   *value=data;
    2026           19061 :   return(TIFFReadDirEntryErrOk);
    2027                 : }
    2028                 : 
    2029               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
    2030                 : {
    2031                 :   enum TIFFReadDirEntryErr err;
    2032                 :   uint32 count;
    2033                 :   void* origdata;
    2034                 :   int64* data;
    2035               0 :   switch (direntry->tdir_type)
    2036                 :   {
    2037                 :     case TIFF_BYTE:
    2038                 :     case TIFF_SBYTE:
    2039                 :     case TIFF_SHORT:
    2040                 :     case TIFF_SSHORT:
    2041                 :     case TIFF_LONG:
    2042                 :     case TIFF_SLONG:
    2043                 :     case TIFF_LONG8:
    2044                 :     case TIFF_SLONG8:
    2045                 :       break;
    2046                 :     default:
    2047               0 :       return(TIFFReadDirEntryErrType);
    2048                 :   }
    2049               0 :   err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
    2050               0 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    2051                 :   {
    2052               0 :     *value=0;
    2053               0 :     return(err);
    2054                 :   }
    2055               0 :   switch (direntry->tdir_type)
    2056                 :   {
    2057                 :     case TIFF_LONG8:
    2058                 :       {
    2059                 :         uint64* m;
    2060                 :         uint32 n;
    2061               0 :         m=(uint64*)origdata;
    2062               0 :         for (n=0; n<count; n++)
    2063                 :         {
    2064               0 :           if (tif->tif_flags&TIFF_SWAB)
    2065               0 :             TIFFSwabLong8(m);
    2066               0 :           err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
    2067               0 :           if (err!=TIFFReadDirEntryErrOk)
    2068                 :           {
    2069               0 :             _TIFFfree(origdata);
    2070               0 :             return(err);
    2071                 :           }
    2072               0 :           m++;
    2073                 :         }
    2074               0 :         *value=(int64*)origdata;
    2075               0 :         return(TIFFReadDirEntryErrOk);
    2076                 :       }
    2077                 :     case TIFF_SLONG8:
    2078               0 :       *value=(int64*)origdata;
    2079               0 :       if (tif->tif_flags&TIFF_SWAB)
    2080               0 :         TIFFSwabArrayOfLong8((uint64*)(*value),count);
    2081               0 :       return(TIFFReadDirEntryErrOk);
    2082                 :   }
    2083               0 :   data=(int64*)_TIFFmalloc(count*8);
    2084               0 :   if (data==0)
    2085                 :   {
    2086               0 :     _TIFFfree(origdata);
    2087               0 :     return(TIFFReadDirEntryErrAlloc);
    2088                 :   }
    2089               0 :   switch (direntry->tdir_type)
    2090                 :   {
    2091                 :     case TIFF_BYTE:
    2092                 :       {
    2093                 :         uint8* ma;
    2094                 :         int64* mb;
    2095                 :         uint32 n;
    2096               0 :         ma=(uint8*)origdata;
    2097               0 :         mb=data;
    2098               0 :         for (n=0; n<count; n++)
    2099               0 :           *mb++=(int64)(*ma++);
    2100                 :       }
    2101               0 :       break;
    2102                 :     case TIFF_SBYTE:
    2103                 :       {
    2104                 :         int8* ma;
    2105                 :         int64* mb;
    2106                 :         uint32 n;
    2107               0 :         ma=(int8*)origdata;
    2108               0 :         mb=data;
    2109               0 :         for (n=0; n<count; n++)
    2110               0 :           *mb++=(int64)(*ma++);
    2111                 :       }
    2112               0 :       break;
    2113                 :     case TIFF_SHORT:
    2114                 :       {
    2115                 :         uint16* ma;
    2116                 :         int64* mb;
    2117                 :         uint32 n;
    2118               0 :         ma=(uint16*)origdata;
    2119               0 :         mb=data;
    2120               0 :         for (n=0; n<count; n++)
    2121                 :         {
    2122               0 :           if (tif->tif_flags&TIFF_SWAB)
    2123               0 :             TIFFSwabShort(ma);
    2124               0 :           *mb++=(int64)(*ma++);
    2125                 :         }
    2126                 :       }
    2127               0 :       break;
    2128                 :     case TIFF_SSHORT:
    2129                 :       {
    2130                 :         int16* ma;
    2131                 :         int64* mb;
    2132                 :         uint32 n;
    2133               0 :         ma=(int16*)origdata;
    2134               0 :         mb=data;
    2135               0 :         for (n=0; n<count; n++)
    2136                 :         {
    2137               0 :           if (tif->tif_flags&TIFF_SWAB)
    2138               0 :             TIFFSwabShort((uint16*)ma);
    2139               0 :           *mb++=(int64)(*ma++);
    2140                 :         }
    2141                 :       }
    2142               0 :       break;
    2143                 :     case TIFF_LONG:
    2144                 :       {
    2145                 :         uint32* ma;
    2146                 :         int64* mb;
    2147                 :         uint32 n;
    2148               0 :         ma=(uint32*)origdata;
    2149               0 :         mb=data;
    2150               0 :         for (n=0; n<count; n++)
    2151                 :         {
    2152               0 :           if (tif->tif_flags&TIFF_SWAB)
    2153               0 :             TIFFSwabLong(ma);
    2154               0 :           *mb++=(int64)(*ma++);
    2155                 :         }
    2156                 :       }
    2157               0 :       break;
    2158                 :     case TIFF_SLONG:
    2159                 :       {
    2160                 :         int32* ma;
    2161                 :         int64* mb;
    2162                 :         uint32 n;
    2163               0 :         ma=(int32*)origdata;
    2164               0 :         mb=data;
    2165               0 :         for (n=0; n<count; n++)
    2166                 :         {
    2167               0 :           if (tif->tif_flags&TIFF_SWAB)
    2168               0 :             TIFFSwabLong((uint32*)ma);
    2169               0 :           *mb++=(int64)(*ma++);
    2170                 :         }
    2171                 :       }
    2172                 :       break;
    2173                 :   }
    2174               0 :   _TIFFfree(origdata);
    2175               0 :   if (err!=TIFFReadDirEntryErrOk)
    2176                 :   {
    2177               0 :     _TIFFfree(data);
    2178               0 :     return(err);
    2179                 :   }
    2180               0 :   *value=data;
    2181               0 :   return(TIFFReadDirEntryErrOk);
    2182                 : }
    2183                 : 
    2184             174 : static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
    2185                 : {
    2186                 :   enum TIFFReadDirEntryErr err;
    2187                 :   uint32 count;
    2188                 :   void* origdata;
    2189                 :   float* data;
    2190             174 :   switch (direntry->tdir_type)
    2191                 :   {
    2192                 :     case TIFF_BYTE:
    2193                 :     case TIFF_SBYTE:
    2194                 :     case TIFF_SHORT:
    2195                 :     case TIFF_SSHORT:
    2196                 :     case TIFF_LONG:
    2197                 :     case TIFF_SLONG:
    2198                 :     case TIFF_LONG8:
    2199                 :     case TIFF_SLONG8:
    2200                 :     case TIFF_RATIONAL:
    2201                 :     case TIFF_SRATIONAL:
    2202                 :     case TIFF_FLOAT:
    2203                 :     case TIFF_DOUBLE:
    2204                 :       break;
    2205                 :     default:
    2206               0 :       return(TIFFReadDirEntryErrType);
    2207                 :   }
    2208             174 :   err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
    2209             348 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    2210                 :   {
    2211               0 :     *value=0;
    2212               0 :     return(err);
    2213                 :   }
    2214             174 :   switch (direntry->tdir_type)
    2215                 :   {
    2216                 :     case TIFF_FLOAT:
    2217               0 :       if (tif->tif_flags&TIFF_SWAB)
    2218               0 :         TIFFSwabArrayOfLong((uint32*)origdata,count);  
    2219                 :       TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
    2220               0 :       *value=(float*)origdata;
    2221               0 :       return(TIFFReadDirEntryErrOk);
    2222                 :   }
    2223             174 :   data=(float*)_TIFFmalloc(count*sizeof(float));
    2224             174 :   if (data==0)
    2225                 :   {
    2226               0 :     _TIFFfree(origdata);
    2227               0 :     return(TIFFReadDirEntryErrAlloc);
    2228                 :   }
    2229             174 :   switch (direntry->tdir_type)
    2230                 :   {
    2231                 :     case TIFF_BYTE:
    2232                 :       {
    2233                 :         uint8* ma;
    2234                 :         float* mb;
    2235                 :         uint32 n;
    2236               0 :         ma=(uint8*)origdata;
    2237               0 :         mb=data;
    2238               0 :         for (n=0; n<count; n++)
    2239               0 :           *mb++=(float)(*ma++);
    2240                 :       }
    2241               0 :       break;
    2242                 :     case TIFF_SBYTE:
    2243                 :       {
    2244                 :         int8* ma;
    2245                 :         float* mb;
    2246                 :         uint32 n;
    2247               0 :         ma=(int8*)origdata;
    2248               0 :         mb=data;
    2249               0 :         for (n=0; n<count; n++)
    2250               0 :           *mb++=(float)(*ma++);
    2251                 :       }
    2252               0 :       break;
    2253                 :     case TIFF_SHORT:
    2254                 :       {
    2255                 :         uint16* ma;
    2256                 :         float* mb;
    2257                 :         uint32 n;
    2258               0 :         ma=(uint16*)origdata;
    2259               0 :         mb=data;
    2260               0 :         for (n=0; n<count; n++)
    2261                 :         {
    2262               0 :           if (tif->tif_flags&TIFF_SWAB)
    2263               0 :             TIFFSwabShort(ma);
    2264               0 :           *mb++=(float)(*ma++);
    2265                 :         }
    2266                 :       }
    2267               0 :       break;
    2268                 :     case TIFF_SSHORT:
    2269                 :       {
    2270                 :         int16* ma;
    2271                 :         float* mb;
    2272                 :         uint32 n;
    2273               0 :         ma=(int16*)origdata;
    2274               0 :         mb=data;
    2275               0 :         for (n=0; n<count; n++)
    2276                 :         {
    2277               0 :           if (tif->tif_flags&TIFF_SWAB)
    2278               0 :             TIFFSwabShort((uint16*)ma);
    2279               0 :           *mb++=(float)(*ma++);
    2280                 :         }
    2281                 :       }
    2282               0 :       break;
    2283                 :     case TIFF_LONG:
    2284                 :       {
    2285                 :         uint32* ma;
    2286                 :         float* mb;
    2287                 :         uint32 n;
    2288               1 :         ma=(uint32*)origdata;
    2289               1 :         mb=data;
    2290               7 :         for (n=0; n<count; n++)
    2291                 :         {
    2292               6 :           if (tif->tif_flags&TIFF_SWAB)
    2293               6 :             TIFFSwabLong(ma);
    2294               6 :           *mb++=(float)(*ma++);
    2295                 :         }
    2296                 :       }
    2297               1 :       break;
    2298                 :     case TIFF_SLONG:
    2299                 :       {
    2300                 :         int32* ma;
    2301                 :         float* mb;
    2302                 :         uint32 n;
    2303               0 :         ma=(int32*)origdata;
    2304               0 :         mb=data;
    2305               0 :         for (n=0; n<count; n++)
    2306                 :         {
    2307               0 :           if (tif->tif_flags&TIFF_SWAB)
    2308               0 :             TIFFSwabLong((uint32*)ma);
    2309               0 :           *mb++=(float)(*ma++);
    2310                 :         }
    2311                 :       }
    2312               0 :       break;
    2313                 :     case TIFF_LONG8:
    2314                 :       {
    2315                 :         uint64* ma;
    2316                 :         float* mb;
    2317                 :         uint32 n;
    2318               0 :         ma=(uint64*)origdata;
    2319               0 :         mb=data;
    2320               0 :         for (n=0; n<count; n++)
    2321                 :         {
    2322               0 :           if (tif->tif_flags&TIFF_SWAB)
    2323               0 :             TIFFSwabLong8(ma);
    2324                 : #if defined(__WIN32__) && (_MSC_VER < 1500)
    2325                 :           /*
    2326                 :            * XXX: MSVC 6.0 does not support
    2327                 :            * conversion of 64-bit integers into
    2328                 :            * floating point values.
    2329                 :            */
    2330                 :           *mb++ = _TIFFUInt64ToFloat(*ma++);
    2331                 : #else
    2332               0 :           *mb++ = (float)(*ma++);
    2333                 : #endif
    2334                 :         }
    2335                 :       }
    2336               0 :       break;
    2337                 :     case TIFF_SLONG8:
    2338                 :       {
    2339                 :         int64* ma;
    2340                 :         float* mb;
    2341                 :         uint32 n;
    2342               0 :         ma=(int64*)origdata;
    2343               0 :         mb=data;
    2344               0 :         for (n=0; n<count; n++)
    2345                 :         {
    2346               0 :           if (tif->tif_flags&TIFF_SWAB)
    2347               0 :             TIFFSwabLong8((uint64*)ma);
    2348               0 :           *mb++=(float)(*ma++);
    2349                 :         }
    2350                 :       }
    2351               0 :       break;
    2352                 :     case TIFF_RATIONAL:
    2353                 :       {
    2354                 :         uint32* ma;
    2355                 :         uint32 maa;
    2356                 :         uint32 mab;
    2357                 :         float* mb;
    2358                 :         uint32 n;
    2359             173 :         ma=(uint32*)origdata;
    2360             173 :         mb=data;
    2361            1208 :         for (n=0; n<count; n++)
    2362                 :         {
    2363            1035 :           if (tif->tif_flags&TIFF_SWAB)
    2364              33 :             TIFFSwabLong(ma);
    2365            1035 :           maa=*ma++;
    2366            1035 :           if (tif->tif_flags&TIFF_SWAB)
    2367              33 :             TIFFSwabLong(ma);
    2368            1035 :           mab=*ma++;
    2369            1035 :           if (mab==0)
    2370               0 :             *mb++=0.0;
    2371                 :           else
    2372            1035 :             *mb++=(float)maa/(float)mab;
    2373                 :         }
    2374                 :       }
    2375             173 :       break;
    2376                 :     case TIFF_SRATIONAL:
    2377                 :       {
    2378                 :         uint32* ma;
    2379                 :         int32 maa;
    2380                 :         uint32 mab;
    2381                 :         float* mb;
    2382                 :         uint32 n;
    2383               0 :         ma=(uint32*)origdata;
    2384               0 :         mb=data;
    2385               0 :         for (n=0; n<count; n++)
    2386                 :         {
    2387               0 :           if (tif->tif_flags&TIFF_SWAB)
    2388               0 :             TIFFSwabLong(ma);
    2389               0 :           maa=*(int32*)ma;
    2390               0 :           ma++;
    2391               0 :           if (tif->tif_flags&TIFF_SWAB)
    2392               0 :             TIFFSwabLong(ma);
    2393               0 :           mab=*ma++;
    2394               0 :           if (mab==0)
    2395               0 :             *mb++=0.0;
    2396                 :           else
    2397               0 :             *mb++=(float)maa/(float)mab;
    2398                 :         }
    2399                 :       }
    2400               0 :       break;
    2401                 :     case TIFF_DOUBLE:
    2402                 :       {
    2403                 :         double* ma;
    2404                 :         float* mb;
    2405                 :         uint32 n;
    2406               0 :         if (tif->tif_flags&TIFF_SWAB)
    2407               0 :           TIFFSwabArrayOfLong8((uint64*)origdata,count);
    2408                 :         TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
    2409               0 :         ma=(double*)origdata;
    2410               0 :         mb=data;
    2411               0 :         for (n=0; n<count; n++)
    2412               0 :           *mb++=(float)(*ma++);
    2413                 :       }
    2414                 :       break;
    2415                 :   }
    2416             174 :   _TIFFfree(origdata);
    2417             174 :   if (err!=TIFFReadDirEntryErrOk)
    2418                 :   {
    2419               0 :     _TIFFfree(data);
    2420               0 :     return(err);
    2421                 :   }
    2422             174 :   *value=data;
    2423             174 :   return(TIFFReadDirEntryErrOk);
    2424                 : }
    2425                 : 
    2426                 : static enum TIFFReadDirEntryErr
    2427           15966 : TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
    2428                 : {
    2429                 :   enum TIFFReadDirEntryErr err;
    2430                 :   uint32 count;
    2431                 :   void* origdata;
    2432                 :   double* data;
    2433           15966 :   switch (direntry->tdir_type)
    2434                 :   {
    2435                 :     case TIFF_BYTE:
    2436                 :     case TIFF_SBYTE:
    2437                 :     case TIFF_SHORT:
    2438                 :     case TIFF_SSHORT:
    2439                 :     case TIFF_LONG:
    2440                 :     case TIFF_SLONG:
    2441                 :     case TIFF_LONG8:
    2442                 :     case TIFF_SLONG8:
    2443                 :     case TIFF_RATIONAL:
    2444                 :     case TIFF_SRATIONAL:
    2445                 :     case TIFF_FLOAT:
    2446                 :     case TIFF_DOUBLE:
    2447                 :       break;
    2448                 :     default:
    2449               0 :       return(TIFFReadDirEntryErrType);
    2450                 :   }
    2451           15966 :   err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
    2452           31932 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    2453                 :   {
    2454               0 :     *value=0;
    2455               0 :     return(err);
    2456                 :   }
    2457           15966 :   switch (direntry->tdir_type)
    2458                 :   {
    2459                 :     case TIFF_DOUBLE:
    2460           15966 :       if (tif->tif_flags&TIFF_SWAB)
    2461             151 :         TIFFSwabArrayOfLong8((uint64*)origdata,count);
    2462                 :       TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
    2463           15966 :       *value=(double*)origdata;
    2464           15966 :       return(TIFFReadDirEntryErrOk);
    2465                 :   }
    2466               0 :   data=(double*)_TIFFmalloc(count*sizeof(double));
    2467               0 :   if (data==0)
    2468                 :   {
    2469               0 :     _TIFFfree(origdata);
    2470               0 :     return(TIFFReadDirEntryErrAlloc);
    2471                 :   }
    2472               0 :   switch (direntry->tdir_type)
    2473                 :   {
    2474                 :     case TIFF_BYTE:
    2475                 :       {
    2476                 :         uint8* ma;
    2477                 :         double* mb;
    2478                 :         uint32 n;
    2479               0 :         ma=(uint8*)origdata;
    2480               0 :         mb=data;
    2481               0 :         for (n=0; n<count; n++)
    2482               0 :           *mb++=(double)(*ma++);
    2483                 :       }
    2484               0 :       break;
    2485                 :     case TIFF_SBYTE:
    2486                 :       {
    2487                 :         int8* ma;
    2488                 :         double* mb;
    2489                 :         uint32 n;
    2490               0 :         ma=(int8*)origdata;
    2491               0 :         mb=data;
    2492               0 :         for (n=0; n<count; n++)
    2493               0 :           *mb++=(double)(*ma++);
    2494                 :       }
    2495               0 :       break;
    2496                 :     case TIFF_SHORT:
    2497                 :       {
    2498                 :         uint16* ma;
    2499                 :         double* mb;
    2500                 :         uint32 n;
    2501               0 :         ma=(uint16*)origdata;
    2502               0 :         mb=data;
    2503               0 :         for (n=0; n<count; n++)
    2504                 :         {
    2505               0 :           if (tif->tif_flags&TIFF_SWAB)
    2506               0 :             TIFFSwabShort(ma);
    2507               0 :           *mb++=(double)(*ma++);
    2508                 :         }
    2509                 :       }
    2510               0 :       break;
    2511                 :     case TIFF_SSHORT:
    2512                 :       {
    2513                 :         int16* ma;
    2514                 :         double* mb;
    2515                 :         uint32 n;
    2516               0 :         ma=(int16*)origdata;
    2517               0 :         mb=data;
    2518               0 :         for (n=0; n<count; n++)
    2519                 :         {
    2520               0 :           if (tif->tif_flags&TIFF_SWAB)
    2521               0 :             TIFFSwabShort((uint16*)ma);
    2522               0 :           *mb++=(double)(*ma++);
    2523                 :         }
    2524                 :       }
    2525               0 :       break;
    2526                 :     case TIFF_LONG:
    2527                 :       {
    2528                 :         uint32* ma;
    2529                 :         double* mb;
    2530                 :         uint32 n;
    2531               0 :         ma=(uint32*)origdata;
    2532               0 :         mb=data;
    2533               0 :         for (n=0; n<count; n++)
    2534                 :         {
    2535               0 :           if (tif->tif_flags&TIFF_SWAB)
    2536               0 :             TIFFSwabLong(ma);
    2537               0 :           *mb++=(double)(*ma++);
    2538                 :         }
    2539                 :       }
    2540               0 :       break;
    2541                 :     case TIFF_SLONG:
    2542                 :       {
    2543                 :         int32* ma;
    2544                 :         double* mb;
    2545                 :         uint32 n;
    2546               0 :         ma=(int32*)origdata;
    2547               0 :         mb=data;
    2548               0 :         for (n=0; n<count; n++)
    2549                 :         {
    2550               0 :           if (tif->tif_flags&TIFF_SWAB)
    2551               0 :             TIFFSwabLong((uint32*)ma);
    2552               0 :           *mb++=(double)(*ma++);
    2553                 :         }
    2554                 :       }
    2555               0 :       break;
    2556                 :     case TIFF_LONG8:
    2557                 :       {
    2558                 :         uint64* ma;
    2559                 :         double* mb;
    2560                 :         uint32 n;
    2561               0 :         ma=(uint64*)origdata;
    2562               0 :         mb=data;
    2563               0 :         for (n=0; n<count; n++)
    2564                 :         {
    2565               0 :           if (tif->tif_flags&TIFF_SWAB)
    2566               0 :             TIFFSwabLong8(ma);
    2567                 : #if defined(__WIN32__) && (_MSC_VER < 1500)
    2568                 :           /*
    2569                 :            * XXX: MSVC 6.0 does not support
    2570                 :            * conversion of 64-bit integers into
    2571                 :            * floating point values.
    2572                 :            */
    2573                 :           *mb++ = _TIFFUInt64ToDouble(*ma++);
    2574                 : #else
    2575               0 :           *mb++ = (double)(*ma++);
    2576                 : #endif
    2577                 :         }
    2578                 :       }
    2579               0 :       break;
    2580                 :     case TIFF_SLONG8:
    2581                 :       {
    2582                 :         int64* ma;
    2583                 :         double* mb;
    2584                 :         uint32 n;
    2585               0 :         ma=(int64*)origdata;
    2586               0 :         mb=data;
    2587               0 :         for (n=0; n<count; n++)
    2588                 :         {
    2589               0 :           if (tif->tif_flags&TIFF_SWAB)
    2590               0 :             TIFFSwabLong8((uint64*)ma);
    2591               0 :           *mb++=(double)(*ma++);
    2592                 :         }
    2593                 :       }
    2594               0 :       break;
    2595                 :     case TIFF_RATIONAL:
    2596                 :       {
    2597                 :         uint32* ma;
    2598                 :         uint32 maa;
    2599                 :         uint32 mab;
    2600                 :         double* mb;
    2601                 :         uint32 n;
    2602               0 :         ma=(uint32*)origdata;
    2603               0 :         mb=data;
    2604               0 :         for (n=0; n<count; n++)
    2605                 :         {
    2606               0 :           if (tif->tif_flags&TIFF_SWAB)
    2607               0 :             TIFFSwabLong(ma);
    2608               0 :           maa=*ma++;
    2609               0 :           if (tif->tif_flags&TIFF_SWAB)
    2610               0 :             TIFFSwabLong(ma);
    2611               0 :           mab=*ma++;
    2612               0 :           if (mab==0)
    2613               0 :             *mb++=0.0;
    2614                 :           else
    2615               0 :             *mb++=(double)maa/(double)mab;
    2616                 :         }
    2617                 :       }
    2618               0 :       break;
    2619                 :     case TIFF_SRATIONAL:
    2620                 :       {
    2621                 :         uint32* ma;
    2622                 :         int32 maa;
    2623                 :         uint32 mab;
    2624                 :         double* mb;
    2625                 :         uint32 n;
    2626               0 :         ma=(uint32*)origdata;
    2627               0 :         mb=data;
    2628               0 :         for (n=0; n<count; n++)
    2629                 :         {
    2630               0 :           if (tif->tif_flags&TIFF_SWAB)
    2631               0 :             TIFFSwabLong(ma);
    2632               0 :           maa=*(int32*)ma;
    2633               0 :           ma++;
    2634               0 :           if (tif->tif_flags&TIFF_SWAB)
    2635               0 :             TIFFSwabLong(ma);
    2636               0 :           mab=*ma++;
    2637               0 :           if (mab==0)
    2638               0 :             *mb++=0.0;
    2639                 :           else
    2640               0 :             *mb++=(double)maa/(double)mab;
    2641                 :         }
    2642                 :       }
    2643               0 :       break;
    2644                 :     case TIFF_FLOAT:
    2645                 :       {
    2646                 :         float* ma;
    2647                 :         double* mb;
    2648                 :         uint32 n;
    2649               0 :         if (tif->tif_flags&TIFF_SWAB)
    2650               0 :           TIFFSwabArrayOfLong((uint32*)origdata,count);  
    2651                 :         TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
    2652               0 :         ma=(float*)origdata;
    2653               0 :         mb=data;
    2654               0 :         for (n=0; n<count; n++)
    2655               0 :           *mb++=(double)(*ma++);
    2656                 :       }
    2657                 :       break;
    2658                 :   }
    2659               0 :   _TIFFfree(origdata);
    2660               0 :   if (err!=TIFFReadDirEntryErrOk)
    2661                 :   {
    2662               0 :     _TIFFfree(data);
    2663               0 :     return(err);
    2664                 :   }
    2665               0 :   *value=data;
    2666               0 :   return(TIFFReadDirEntryErrOk);
    2667                 : }
    2668                 : 
    2669               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
    2670                 : {
    2671                 :   enum TIFFReadDirEntryErr err;
    2672                 :   uint32 count;
    2673                 :   void* origdata;
    2674                 :   uint64* data;
    2675               0 :   switch (direntry->tdir_type)
    2676                 :   {
    2677                 :     case TIFF_LONG:
    2678                 :     case TIFF_LONG8:
    2679                 :     case TIFF_IFD:
    2680                 :     case TIFF_IFD8:
    2681                 :       break;
    2682                 :     default:
    2683               0 :       return(TIFFReadDirEntryErrType);
    2684                 :   }
    2685               0 :   err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
    2686               0 :   if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
    2687                 :   {
    2688               0 :     *value=0;
    2689               0 :     return(err);
    2690                 :   }
    2691               0 :   switch (direntry->tdir_type)
    2692                 :   {
    2693                 :     case TIFF_LONG8:
    2694                 :     case TIFF_IFD8:
    2695               0 :       *value=(uint64*)origdata;
    2696               0 :       if (tif->tif_flags&TIFF_SWAB)
    2697               0 :         TIFFSwabArrayOfLong8(*value,count);
    2698               0 :       return(TIFFReadDirEntryErrOk);
    2699                 :   }
    2700               0 :   data=(uint64*)_TIFFmalloc(count*8);
    2701               0 :   if (data==0)
    2702                 :   {
    2703               0 :     _TIFFfree(origdata);
    2704               0 :     return(TIFFReadDirEntryErrAlloc);
    2705                 :   }
    2706               0 :   switch (direntry->tdir_type)
    2707                 :   {
    2708                 :     case TIFF_LONG:
    2709                 :     case TIFF_IFD:
    2710                 :       {
    2711                 :         uint32* ma;
    2712                 :         uint64* mb;
    2713                 :         uint32 n;
    2714               0 :         ma=(uint32*)origdata;
    2715               0 :         mb=data;
    2716               0 :         for (n=0; n<count; n++)
    2717                 :         {
    2718               0 :           if (tif->tif_flags&TIFF_SWAB)
    2719               0 :             TIFFSwabLong(ma);
    2720               0 :           *mb++=(uint64)(*ma++);
    2721                 :         }
    2722                 :       }
    2723                 :       break;
    2724                 :   }
    2725               0 :   _TIFFfree(origdata);
    2726               0 :   if (err!=TIFFReadDirEntryErrOk)
    2727                 :   {
    2728               0 :     _TIFFfree(data);
    2729               0 :     return(err);
    2730                 :   }
    2731               0 :   *value=data;
    2732               0 :   return(TIFFReadDirEntryErrOk);
    2733                 : }
    2734                 : 
    2735            3672 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
    2736                 : {
    2737                 :   enum TIFFReadDirEntryErr err;
    2738                 :   uint16* m;
    2739                 :   uint16* na;
    2740                 :   uint16 nb;
    2741            3672 :   if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
    2742               0 :     return(TIFFReadDirEntryErrCount);
    2743            3672 :   err=TIFFReadDirEntryShortArray(tif,direntry,&m);
    2744            3672 :   if (err!=TIFFReadDirEntryErrOk)
    2745               0 :     return(err);
    2746            3672 :   na=m;
    2747            3672 :   nb=tif->tif_dir.td_samplesperpixel;
    2748            3672 :   *value=*na++;
    2749            3672 :   nb--;
    2750         1063325 :   while (nb>0)
    2751                 :   {
    2752         1055981 :     if (*na++!=*value)
    2753                 :     {
    2754               0 :       err=TIFFReadDirEntryErrPsdif;
    2755               0 :       break;
    2756                 :     }
    2757         1055981 :     nb--;
    2758                 :   }
    2759            3672 :   _TIFFfree(m);
    2760            3672 :   return(err);
    2761                 : }
    2762                 : 
    2763                 : #if 0
    2764                 : static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
    2765                 : {
    2766                 :   enum TIFFReadDirEntryErr err;
    2767                 :   double* m;
    2768                 :   double* na;
    2769                 :   uint16 nb;
    2770                 :   if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
    2771                 :     return(TIFFReadDirEntryErrCount);
    2772                 :   err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
    2773                 :   if (err!=TIFFReadDirEntryErrOk)
    2774                 :     return(err);
    2775                 :   na=m;
    2776                 :   nb=tif->tif_dir.td_samplesperpixel;
    2777                 :   *value=*na++;
    2778                 :   nb--;
    2779                 :   while (nb>0)
    2780                 :   {
    2781                 :     if (*na++!=*value)
    2782                 :     {
    2783                 :       err=TIFFReadDirEntryErrPsdif;
    2784                 :       break;
    2785                 :     }
    2786                 :     nb--;
    2787                 :   }
    2788                 :   _TIFFfree(m);
    2789                 :   return(err);
    2790                 : }
    2791                 : #endif
    2792                 : 
    2793               0 : static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
    2794                 : {
    2795                 :   (void) tif;
    2796               0 :   *value=*(uint8*)(&direntry->tdir_offset);
    2797               0 : }
    2798                 : 
    2799               0 : static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
    2800                 : {
    2801                 :   (void) tif;
    2802               0 :   *value=*(int8*)(&direntry->tdir_offset);
    2803               0 : }
    2804                 : 
    2805           90625 : static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
    2806                 : {
    2807           90625 :   *value = direntry->tdir_offset.toff_short;
    2808                 :   /* *value=*(uint16*)(&direntry->tdir_offset); */
    2809           90625 :   if (tif->tif_flags&TIFF_SWAB)
    2810            3301 :     TIFFSwabShort(value);
    2811           90625 : }
    2812                 : 
    2813               0 : static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
    2814                 : {
    2815               0 :   *value=*(int16*)(&direntry->tdir_offset);
    2816               0 :   if (tif->tif_flags&TIFF_SWAB)
    2817               0 :     TIFFSwabShort((uint16*)value);
    2818               0 : }
    2819                 : 
    2820            2255 : static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
    2821                 : {
    2822            2255 :   *value=*(uint32*)(&direntry->tdir_offset);
    2823            2255 :   if (tif->tif_flags&TIFF_SWAB)
    2824             220 :     TIFFSwabLong(value);
    2825            2255 : }
    2826                 : 
    2827               0 : static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
    2828                 : {
    2829               0 :   *value=*(int32*)(&direntry->tdir_offset);
    2830               0 :   if (tif->tif_flags&TIFF_SWAB)
    2831               0 :     TIFFSwabLong((uint32*)value);
    2832               0 : }
    2833                 : 
    2834               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
    2835                 : {
    2836               0 :   if (!(tif->tif_flags&TIFF_BIGTIFF))
    2837                 :   {
    2838                 :     enum TIFFReadDirEntryErr err;
    2839               0 :     uint32 offset = direntry->tdir_offset.toff_long;
    2840               0 :     if (tif->tif_flags&TIFF_SWAB)
    2841               0 :       TIFFSwabLong(&offset);
    2842               0 :     err=TIFFReadDirEntryData(tif,offset,8,value);
    2843               0 :     if (err!=TIFFReadDirEntryErrOk)
    2844               0 :       return(err);
    2845                 :   }
    2846                 :   else
    2847               0 :     *value = direntry->tdir_offset.toff_long8;
    2848               0 :   if (tif->tif_flags&TIFF_SWAB)
    2849               0 :     TIFFSwabLong8(value);
    2850               0 :   return(TIFFReadDirEntryErrOk);
    2851                 : }
    2852                 : 
    2853               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
    2854                 : {
    2855               0 :   if (!(tif->tif_flags&TIFF_BIGTIFF))
    2856                 :   {
    2857                 :     enum TIFFReadDirEntryErr err;
    2858               0 :     uint32 offset = direntry->tdir_offset.toff_long;
    2859               0 :     if (tif->tif_flags&TIFF_SWAB)
    2860               0 :       TIFFSwabLong(&offset);
    2861               0 :     err=TIFFReadDirEntryData(tif,offset,8,value);
    2862               0 :     if (err!=TIFFReadDirEntryErrOk)
    2863               0 :       return(err);
    2864                 :   }
    2865                 :   else
    2866               0 :     *value=*(int64*)(&direntry->tdir_offset);
    2867               0 :   if (tif->tif_flags&TIFF_SWAB)
    2868               0 :     TIFFSwabLong8((uint64*)value);
    2869               0 :   return(TIFFReadDirEntryErrOk);
    2870                 : }
    2871                 : 
    2872             130 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
    2873                 : {
    2874                 :   UInt64Aligned_t m;
    2875                 : 
    2876                 :   assert(sizeof(double)==8);
    2877                 :   assert(sizeof(uint64)==8);
    2878                 :   assert(sizeof(uint32)==4);
    2879             130 :   if (!(tif->tif_flags&TIFF_BIGTIFF))
    2880                 :   {
    2881                 :     enum TIFFReadDirEntryErr err;
    2882             130 :     uint32 offset = direntry->tdir_offset.toff_long;
    2883             130 :     if (tif->tif_flags&TIFF_SWAB)
    2884               8 :       TIFFSwabLong(&offset);
    2885             130 :     err=TIFFReadDirEntryData(tif,offset,8,m.i);
    2886             130 :     if (err!=TIFFReadDirEntryErrOk)
    2887               0 :       return(err);
    2888                 :   }
    2889                 :   else
    2890               0 :     m.l = direntry->tdir_offset.toff_long8;
    2891             130 :   if (tif->tif_flags&TIFF_SWAB)
    2892               8 :     TIFFSwabArrayOfLong(m.i,2);
    2893             130 :   if (m.i[0]==0)
    2894              14 :     *value=0.0;
    2895                 :   else
    2896             116 :     *value=(double)m.i[0]/(double)m.i[1];
    2897             130 :   return(TIFFReadDirEntryErrOk);
    2898                 : }
    2899                 : 
    2900               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
    2901                 : {
    2902                 :   UInt64Aligned_t m;
    2903                 :   assert(sizeof(double)==8);
    2904                 :   assert(sizeof(uint64)==8);
    2905                 :   assert(sizeof(int32)==4);
    2906                 :   assert(sizeof(uint32)==4);
    2907               0 :   if (!(tif->tif_flags&TIFF_BIGTIFF))
    2908                 :   {
    2909                 :     enum TIFFReadDirEntryErr err;
    2910               0 :     uint32 offset = direntry->tdir_offset.toff_long;
    2911               0 :     if (tif->tif_flags&TIFF_SWAB)
    2912               0 :       TIFFSwabLong(&offset);
    2913               0 :     err=TIFFReadDirEntryData(tif,offset,8,m.i);
    2914               0 :     if (err!=TIFFReadDirEntryErrOk)
    2915               0 :       return(err);
    2916                 :   }
    2917                 :   else
    2918               0 :     m.l=direntry->tdir_offset.toff_long8;
    2919               0 :   if (tif->tif_flags&TIFF_SWAB)
    2920               0 :     TIFFSwabArrayOfLong(m.i,2);
    2921               0 :   if ((int32)m.i[0]==0)
    2922               0 :     *value=0.0;
    2923                 :   else
    2924               0 :     *value=(double)((int32)m.i[0])/(double)m.i[1];
    2925               0 :   return(TIFFReadDirEntryErrOk);
    2926                 : }
    2927                 : 
    2928               0 : static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
    2929                 : {
    2930                 :          union
    2931                 :    {
    2932                 :      float  f;
    2933                 :      uint32 i;
    2934                 :    } float_union;
    2935                 :   assert(sizeof(float)==4);
    2936                 :   assert(sizeof(uint32)==4);
    2937                 :   assert(sizeof(float_union)==4);
    2938               0 :   float_union.i=*(uint32*)(&direntry->tdir_offset);
    2939               0 :   *value=float_union.f;
    2940               0 :   if (tif->tif_flags&TIFF_SWAB)
    2941               0 :     TIFFSwabLong((uint32*)value);
    2942               0 : }
    2943                 : 
    2944               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
    2945                 : {
    2946                 :   assert(sizeof(double)==8);
    2947                 :   assert(sizeof(uint64)==8);
    2948                 :   assert(sizeof(UInt64Aligned_t)==8);
    2949               0 :   if (!(tif->tif_flags&TIFF_BIGTIFF))
    2950                 :   {
    2951                 :     enum TIFFReadDirEntryErr err;
    2952               0 :     uint32 offset = direntry->tdir_offset.toff_long;
    2953               0 :     if (tif->tif_flags&TIFF_SWAB)
    2954               0 :       TIFFSwabLong(&offset);
    2955               0 :     err=TIFFReadDirEntryData(tif,offset,8,value);
    2956               0 :     if (err!=TIFFReadDirEntryErrOk)
    2957               0 :       return(err);
    2958                 :   }
    2959                 :   else
    2960                 :   {
    2961                 :          UInt64Aligned_t uint64_union;
    2962               0 :          uint64_union.l=direntry->tdir_offset.toff_long8;
    2963               0 :          *value=uint64_union.d;
    2964                 :   }
    2965               0 :   if (tif->tif_flags&TIFF_SWAB)
    2966               0 :     TIFFSwabLong8((uint64*)value);
    2967               0 :   return(TIFFReadDirEntryErrOk);
    2968                 : }
    2969                 : 
    2970               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
    2971                 : {
    2972               0 :   if (value<0)
    2973               0 :     return(TIFFReadDirEntryErrRange);
    2974                 :   else
    2975               0 :     return(TIFFReadDirEntryErrOk);
    2976                 : }
    2977                 : 
    2978               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
    2979                 : {
    2980               0 :   if (value>0xFF)
    2981               0 :     return(TIFFReadDirEntryErrRange);
    2982                 :   else
    2983               0 :     return(TIFFReadDirEntryErrOk);
    2984                 : }
    2985                 : 
    2986               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
    2987                 : {
    2988               0 :   if ((value<0)||(value>0xFF))
    2989               0 :     return(TIFFReadDirEntryErrRange);
    2990                 :   else
    2991               0 :     return(TIFFReadDirEntryErrOk);
    2992                 : }
    2993                 : 
    2994               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
    2995                 : {
    2996               0 :   if (value>0xFF)
    2997               0 :     return(TIFFReadDirEntryErrRange);
    2998                 :   else
    2999               0 :     return(TIFFReadDirEntryErrOk);
    3000                 : }
    3001                 : 
    3002               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
    3003                 : {
    3004               0 :   if ((value<0)||(value>0xFF))
    3005               0 :     return(TIFFReadDirEntryErrRange);
    3006                 :   else
    3007               0 :     return(TIFFReadDirEntryErrOk);
    3008                 : }
    3009                 : 
    3010               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
    3011                 : {
    3012               0 :   if (value>0xFF)
    3013               0 :     return(TIFFReadDirEntryErrRange);
    3014                 :   else
    3015               0 :     return(TIFFReadDirEntryErrOk);
    3016                 : }
    3017                 : 
    3018               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
    3019                 : {
    3020               0 :   if ((value<0)||(value>0xFF))
    3021               0 :     return(TIFFReadDirEntryErrRange);
    3022                 :   else
    3023               0 :     return(TIFFReadDirEntryErrOk);
    3024                 : }
    3025                 : 
    3026               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
    3027                 : {
    3028               0 :   if (value>0x7F)
    3029               0 :     return(TIFFReadDirEntryErrRange);
    3030                 :   else
    3031               0 :     return(TIFFReadDirEntryErrOk);
    3032                 : }
    3033                 : 
    3034               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
    3035                 : {
    3036               0 :   if (value>0x7F)
    3037               0 :     return(TIFFReadDirEntryErrRange);
    3038                 :   else
    3039               0 :     return(TIFFReadDirEntryErrOk);
    3040                 : }
    3041                 : 
    3042               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
    3043                 : {
    3044               0 :   if ((value<-0x80)||(value>0x7F))
    3045               0 :     return(TIFFReadDirEntryErrRange);
    3046                 :   else
    3047               0 :     return(TIFFReadDirEntryErrOk);
    3048                 : }
    3049                 : 
    3050               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
    3051                 : {
    3052               0 :   if (value>0x7F)
    3053               0 :     return(TIFFReadDirEntryErrRange);
    3054                 :   else
    3055               0 :     return(TIFFReadDirEntryErrOk);
    3056                 : }
    3057                 : 
    3058               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
    3059                 : {
    3060               0 :   if ((value<-0x80)||(value>0x7F))
    3061               0 :     return(TIFFReadDirEntryErrRange);
    3062                 :   else
    3063               0 :     return(TIFFReadDirEntryErrOk);
    3064                 : }
    3065                 : 
    3066               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
    3067                 : {
    3068               0 :   if (value>0x7F)
    3069               0 :     return(TIFFReadDirEntryErrRange);
    3070                 :   else
    3071               0 :     return(TIFFReadDirEntryErrOk);
    3072                 : }
    3073                 : 
    3074               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
    3075                 : {
    3076               0 :   if ((value<-0x80)||(value>0x7F))
    3077               0 :     return(TIFFReadDirEntryErrRange);
    3078                 :   else
    3079               0 :     return(TIFFReadDirEntryErrOk);
    3080                 : }
    3081                 : 
    3082               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
    3083                 : {
    3084               0 :   if (value<0)
    3085               0 :     return(TIFFReadDirEntryErrRange);
    3086                 :   else
    3087               0 :     return(TIFFReadDirEntryErrOk);
    3088                 : }
    3089                 : 
    3090               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
    3091                 : {
    3092               0 :   if (value<0)
    3093               0 :     return(TIFFReadDirEntryErrRange);
    3094                 :   else
    3095               0 :     return(TIFFReadDirEntryErrOk);
    3096                 : }
    3097                 : 
    3098               1 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
    3099                 : {
    3100               1 :   if (value>0xFFFF)
    3101               0 :     return(TIFFReadDirEntryErrRange);
    3102                 :   else
    3103               1 :     return(TIFFReadDirEntryErrOk);
    3104                 : }
    3105                 : 
    3106               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
    3107                 : {
    3108               0 :   if ((value<0)||(value>0xFFFF))
    3109               0 :     return(TIFFReadDirEntryErrRange);
    3110                 :   else
    3111               0 :     return(TIFFReadDirEntryErrOk);
    3112                 : }
    3113                 : 
    3114               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
    3115                 : {
    3116               0 :   if (value>0xFFFF)
    3117               0 :     return(TIFFReadDirEntryErrRange);
    3118                 :   else
    3119               0 :     return(TIFFReadDirEntryErrOk);
    3120                 : }
    3121                 : 
    3122               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
    3123                 : {
    3124               0 :   if ((value<0)||(value>0xFFFF))
    3125               0 :     return(TIFFReadDirEntryErrRange);
    3126                 :   else
    3127               0 :     return(TIFFReadDirEntryErrOk);
    3128                 : }
    3129                 : 
    3130               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
    3131                 : {
    3132               0 :   if (value>0x7FFF)
    3133               0 :     return(TIFFReadDirEntryErrRange);
    3134                 :   else
    3135               0 :     return(TIFFReadDirEntryErrOk);
    3136                 : }
    3137                 : 
    3138               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
    3139                 : {
    3140               0 :   if (value>0x7FFF)
    3141               0 :     return(TIFFReadDirEntryErrRange);
    3142                 :   else
    3143               0 :     return(TIFFReadDirEntryErrOk);
    3144                 : }
    3145                 : 
    3146               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
    3147                 : {
    3148               0 :   if ((value<-0x8000)||(value>0x7FFF))
    3149               0 :     return(TIFFReadDirEntryErrRange);
    3150                 :   else
    3151               0 :     return(TIFFReadDirEntryErrOk);
    3152                 : }
    3153                 : 
    3154               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
    3155                 : {
    3156               0 :   if (value>0x7FFF)
    3157               0 :     return(TIFFReadDirEntryErrRange);
    3158                 :   else
    3159               0 :     return(TIFFReadDirEntryErrOk);
    3160                 : }
    3161                 : 
    3162               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
    3163                 : {
    3164               0 :   if ((value<-0x8000)||(value>0x7FFF))
    3165               0 :     return(TIFFReadDirEntryErrRange);
    3166                 :   else
    3167               0 :     return(TIFFReadDirEntryErrOk);
    3168                 : }
    3169                 : 
    3170               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
    3171                 : {
    3172               0 :   if (value<0)
    3173               0 :     return(TIFFReadDirEntryErrRange);
    3174                 :   else
    3175               0 :     return(TIFFReadDirEntryErrOk);
    3176                 : }
    3177                 : 
    3178               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
    3179                 : {
    3180               0 :   if (value<0)
    3181               0 :     return(TIFFReadDirEntryErrRange);
    3182                 :   else
    3183               0 :     return(TIFFReadDirEntryErrOk);
    3184                 : }
    3185                 : 
    3186               0 : static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
    3187                 : {
    3188               0 :   if (value<0)
    3189               0 :     return(TIFFReadDirEntryErrRange);
    3190                 :   else
    3191               0 :     return(TIFFReadDirEntryErrOk);
    3192                 : }
    3193                 : 
    3194                 : /*
    3195                 :  * Largest 32-bit unsigned integer value.
    3196                 :  */
    3197                 : #if defined(__WIN32__) && defined(_MSC_VER)
    3198                 : # define TIFF_UINT32_MAX 0xFFFFFFFFI64
    3199                 : #else
    3200                 : # define TIFF_UINT32_MAX 0xFFFFFFFFLL
    3201                 : #endif
    3202                 : 
    3203                 : static enum TIFFReadDirEntryErr
    3204               0 : TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
    3205                 : {
    3206               0 :   if (value > TIFF_UINT32_MAX)
    3207               0 :     return(TIFFReadDirEntryErrRange);
    3208                 :   else
    3209               0 :     return(TIFFReadDirEntryErrOk);
    3210                 : }
    3211                 : 
    3212                 : static enum TIFFReadDirEntryErr
    3213               0 : TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
    3214                 : {
    3215               0 :   if ((value<0) || (value > TIFF_UINT32_MAX))
    3216               0 :     return(TIFFReadDirEntryErrRange);
    3217                 :   else
    3218               0 :     return(TIFFReadDirEntryErrOk);
    3219                 : }
    3220                 : 
    3221                 : #undef TIFF_UINT32_MAX
    3222                 : 
    3223                 : static enum TIFFReadDirEntryErr
    3224               0 : TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
    3225                 : {
    3226               0 :   if (value > 0x7FFFFFFFUL)
    3227               0 :     return(TIFFReadDirEntryErrRange);
    3228                 :   else
    3229               0 :     return(TIFFReadDirEntryErrOk);
    3230                 : }
    3231                 : 
    3232                 : static enum TIFFReadDirEntryErr
    3233               0 : TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
    3234                 : {
    3235               0 :   if (value > 0x7FFFFFFFUL)
    3236               0 :     return(TIFFReadDirEntryErrRange);
    3237                 :   else
    3238               0 :     return(TIFFReadDirEntryErrOk);
    3239                 : }
    3240                 : 
    3241                 : static enum TIFFReadDirEntryErr
    3242               0 : TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
    3243                 : {
    3244               0 :   if ((value < 0L-0x80000000L) || (value > 0x7FFFFFFFL))
    3245               0 :     return(TIFFReadDirEntryErrRange);
    3246                 :   else
    3247               0 :     return(TIFFReadDirEntryErrOk);
    3248                 : }
    3249                 : 
    3250                 : static enum TIFFReadDirEntryErr
    3251               0 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
    3252                 : {
    3253               0 :   if (value < 0)
    3254               0 :     return(TIFFReadDirEntryErrRange);
    3255                 :   else
    3256               0 :     return(TIFFReadDirEntryErrOk);
    3257                 : }
    3258                 : 
    3259                 : static enum TIFFReadDirEntryErr
    3260               0 : TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
    3261                 : {
    3262               0 :   if (value < 0)
    3263               0 :     return(TIFFReadDirEntryErrRange);
    3264                 :   else
    3265               0 :     return(TIFFReadDirEntryErrOk);
    3266                 : }
    3267                 : 
    3268                 : static enum TIFFReadDirEntryErr
    3269               0 : TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
    3270                 : {
    3271               0 :   if (value < 0)
    3272               0 :     return(TIFFReadDirEntryErrRange);
    3273                 :   else
    3274               0 :     return(TIFFReadDirEntryErrOk);
    3275                 : }
    3276                 : 
    3277                 : static enum TIFFReadDirEntryErr
    3278               0 : TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
    3279                 : {
    3280               0 :   if (value < 0)
    3281               0 :     return(TIFFReadDirEntryErrRange);
    3282                 :   else
    3283               0 :     return(TIFFReadDirEntryErrOk);
    3284                 : }
    3285                 : 
    3286                 : /*
    3287                 :  * Largest 64-bit signed integer value.
    3288                 :  */
    3289                 : #if defined(__WIN32__) && defined(_MSC_VER)
    3290                 : # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFI64
    3291                 : #else
    3292                 : # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFLL
    3293                 : #endif
    3294                 : 
    3295                 : static enum TIFFReadDirEntryErr
    3296               0 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
    3297                 : {
    3298               0 :   if (value > TIFF_INT64_MAX)
    3299               0 :     return(TIFFReadDirEntryErrRange);
    3300                 :   else
    3301               0 :     return(TIFFReadDirEntryErrOk);
    3302                 : }
    3303                 : 
    3304                 : #undef TIFF_INT64_MAX
    3305                 : 
    3306                 : static enum TIFFReadDirEntryErr
    3307           35477 : TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
    3308                 : {
    3309           35477 :   assert(size>0);
    3310           35477 :   if (!isMapped(tif)) {
    3311           35477 :     if (!SeekOK(tif,offset))
    3312               0 :       return(TIFFReadDirEntryErrIo);
    3313           35477 :     if (!ReadOK(tif,dest,size))
    3314               2 :       return(TIFFReadDirEntryErrIo);
    3315                 :   } else {
    3316                 :     size_t ma,mb;
    3317               0 :     ma=(size_t)offset;
    3318               0 :     mb=ma+size;
    3319               0 :     if (((uint64)ma!=offset)
    3320                 :         || (mb < ma)
    3321               0 :         || (mb - ma != (size_t) size)
    3322               0 :         || (mb < (size_t)size)
    3323               0 :         || (mb > (size_t)tif->tif_size)
    3324                 :         )
    3325               0 :       return(TIFFReadDirEntryErrIo);
    3326               0 :     _TIFFmemcpy(dest,tif->tif_base+ma,size);
    3327                 :   }
    3328           35475 :   return(TIFFReadDirEntryErrOk);
    3329                 : }
    3330                 : 
    3331               2 : static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
    3332                 : {
    3333               2 :   if (!recover) {
    3334               0 :     switch (err) {
    3335                 :       case TIFFReadDirEntryErrCount:
    3336               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3337                 :                "Incorrect count for \"%s\"",
    3338                 :                tagname);
    3339               0 :         break;
    3340                 :       case TIFFReadDirEntryErrType:
    3341               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3342                 :                "Incompatible type for \"%s\"",
    3343                 :                tagname);
    3344               0 :         break;
    3345                 :       case TIFFReadDirEntryErrIo:
    3346               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3347                 :                "IO error during reading of \"%s\"",
    3348                 :                tagname);
    3349               0 :         break;
    3350                 :       case TIFFReadDirEntryErrRange:
    3351               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3352                 :                "Incorrect value for \"%s\"",
    3353                 :                tagname);
    3354               0 :         break;
    3355                 :       case TIFFReadDirEntryErrPsdif:
    3356               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3357                 :       "Cannot handle different values per sample for \"%s\"",
    3358                 :                tagname);
    3359               0 :         break;
    3360                 :       case TIFFReadDirEntryErrSizesan:
    3361               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3362                 :         "Sanity check on size of \"%s\" value failed",
    3363                 :                tagname);
    3364               0 :         break;
    3365                 :       case TIFFReadDirEntryErrAlloc:
    3366               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3367                 :                "Out of memory reading of \"%s\"",
    3368                 :                tagname);
    3369               0 :         break;
    3370                 :       default:
    3371               0 :         assert(0);   /* we should never get here */
    3372                 :         break;
    3373                 :     }
    3374                 :   } else {
    3375               2 :     switch (err) {
    3376                 :       case TIFFReadDirEntryErrCount:
    3377               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    3378                 :         "Incorrect count for \"%s\"; tag ignored",
    3379                 :                tagname);
    3380               0 :         break;
    3381                 :       case TIFFReadDirEntryErrType:
    3382               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    3383                 :         "Incompatible type for \"%s\"; tag ignored",
    3384                 :                  tagname);
    3385               0 :         break;
    3386                 :       case TIFFReadDirEntryErrIo:
    3387               2 :         TIFFWarningExt(tif->tif_clientdata, module,
    3388                 :       "IO error during reading of \"%s\"; tag ignored",
    3389                 :                  tagname);
    3390               2 :         break;
    3391                 :       case TIFFReadDirEntryErrRange:
    3392               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    3393                 :         "Incorrect value for \"%s\"; tag ignored",
    3394                 :                  tagname);
    3395               0 :         break;
    3396                 :       case TIFFReadDirEntryErrPsdif:
    3397               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    3398                 :   "Cannot handle different values per sample for \"%s\"; tag ignored",
    3399                 :                  tagname);
    3400               0 :         break;
    3401                 :       case TIFFReadDirEntryErrSizesan:
    3402               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    3403                 :     "Sanity check on size of \"%s\" value failed; tag ignored",
    3404                 :                  tagname);
    3405               0 :         break;
    3406                 :       case TIFFReadDirEntryErrAlloc:
    3407               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    3408                 :         "Out of memory reading of \"%s\"; tag ignored",
    3409                 :                  tagname);
    3410               0 :         break;
    3411                 :       default:
    3412               0 :         assert(0);   /* we should never get here */
    3413                 :         break;
    3414                 :     }
    3415                 :   }
    3416               2 : }
    3417                 : 
    3418                 : /*
    3419                 :  * Read the next TIFF directory from a file and convert it to the internal
    3420                 :  * format. We read directories sequentially.
    3421                 :  */
    3422                 : int
    3423           10303 : TIFFReadDirectory(TIFF* tif)
    3424                 : {
    3425                 :   static const char module[] = "TIFFReadDirectory";
    3426                 :   TIFFDirEntry* dir;
    3427                 :   uint16 dircount;
    3428                 :   TIFFDirEntry* dp;
    3429                 :   uint16 di;
    3430                 :   const TIFFField* fip;
    3431           10303 :   uint32 fii=FAILED_FII;
    3432                 :         toff_t nextdiroff;
    3433           10303 :   tif->tif_diroff=tif->tif_nextdiroff;
    3434           10303 :   if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
    3435              97 :     return 0;           /* last offset or bad offset (IFD looping) */
    3436           10206 :   (*tif->tif_cleanup)(tif);   /* cleanup any previous compression state */
    3437           10206 :   tif->tif_curdir++;
    3438           10206 :         nextdiroff = tif->tif_nextdiroff;
    3439           10206 :   dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
    3440           10206 :   if (!dircount)
    3441                 :   {
    3442               0 :     TIFFErrorExt(tif->tif_clientdata,module,
    3443                 :         "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
    3444               0 :     return 0;
    3445                 :   }
    3446           10206 :   TIFFReadDirectoryCheckOrder(tif,dir,dircount);
    3447                 : 
    3448                 :         /*
    3449                 :          * Mark duplicates of any tag to be ignored (bugzilla 1994)
    3450                 :          * to avoid certain pathological problems.
    3451                 :          */
    3452                 :   {
    3453                 :     TIFFDirEntry* ma;
    3454                 :     uint16 mb;
    3455          158222 :     for (ma=dir, mb=0; mb<dircount; ma++, mb++)
    3456                 :     {
    3457                 :       TIFFDirEntry* na;
    3458                 :       uint16 nb;
    3459         1162166 :       for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
    3460                 :       {
    3461         1014150 :         if (ma->tdir_tag==na->tdir_tag)
    3462               0 :           na->tdir_tag=IGNORE;
    3463                 :       }
    3464                 :     }
    3465                 :   }
    3466                 :         
    3467           10206 :   tif->tif_flags &= ~TIFF_BEENWRITING;    /* reset before new dir */
    3468           10206 :   tif->tif_flags &= ~TIFF_BUF4WRITE;      /* reset before new dir */
    3469                 :   /* free any old stuff and reinit */
    3470           10206 :   TIFFFreeDirectory(tif);
    3471           10206 :   TIFFDefaultDirectory(tif);
    3472                 :   /*
    3473                 :    * Electronic Arts writes gray-scale TIFF files
    3474                 :    * without a PlanarConfiguration directory entry.
    3475                 :    * Thus we setup a default value here, even though
    3476                 :    * the TIFF spec says there is no default value.
    3477                 :    */
    3478           10206 :   TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
    3479                 :   /*
    3480                 :    * Setup default value and then make a pass over
    3481                 :    * the fields to check type and tag information,
    3482                 :    * and to extract info required to size data
    3483                 :    * structures.  A second pass is made afterwards
    3484                 :    * to read in everthing not taken in the first pass.
    3485                 :    * But we must process the Compression tag first
    3486                 :    * in order to merge in codec-private tag definitions (otherwise
    3487                 :    * we may get complaints about unknown tags).  However, the
    3488                 :    * Compression tag may be dependent on the SamplesPerPixel
    3489                 :    * tag value because older TIFF specs permited Compression
    3490                 :    * to be written as a SamplesPerPixel-count tag entry.
    3491                 :    * Thus if we don't first figure out the correct SamplesPerPixel
    3492                 :    * tag value then we may end up ignoring the Compression tag
    3493                 :    * value because it has an incorrect count value (if the
    3494                 :    * true value of SamplesPerPixel is not 1).
    3495                 :    */
    3496           10206 :   dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
    3497           10206 :   if (dp)
    3498                 :   {
    3499           10198 :     if (!TIFFFetchNormalTag(tif,dp,0))
    3500               0 :       goto bad;
    3501           10198 :     dp->tdir_tag=IGNORE;
    3502                 :   }
    3503           10206 :   dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
    3504           10206 :   if (dp)
    3505                 :   {
    3506                 :     /*
    3507                 :      * The 5.0 spec says the Compression tag has one value, while
    3508                 :      * earlier specs say it has one value per sample.  Because of
    3509                 :      * this, we accept the tag if one value is supplied with either
    3510                 :      * count.
    3511                 :      */
    3512                 :     uint16 value;
    3513                 :     enum TIFFReadDirEntryErr err;
    3514           10204 :     err=TIFFReadDirEntryShort(tif,dp,&value);
    3515           10204 :     if (err==TIFFReadDirEntryErrCount)
    3516               0 :       err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
    3517           10204 :     if (err!=TIFFReadDirEntryErrOk)
    3518                 :     {
    3519               0 :       TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
    3520               0 :       goto bad;
    3521                 :     }
    3522           10204 :     if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
    3523               0 :       goto bad;
    3524           10204 :     dp->tdir_tag=IGNORE;
    3525                 :   }
    3526                 :   else
    3527                 :   {
    3528               2 :     if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
    3529               0 :       goto bad;
    3530                 :   }
    3531                 :   /*
    3532                 :    * First real pass over the directory.
    3533                 :    */
    3534          158222 :   for (di=0, dp=dir; di<dircount; di++, dp++)
    3535                 :   {
    3536          148016 :     if (dp->tdir_tag!=IGNORE)
    3537                 :     {
    3538          127614 :       TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
    3539          127614 :       if (fii == FAILED_FII)
    3540                 :       {
    3541               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    3542                 :             "Unknown field with tag %d (0x%x) encountered",
    3543               0 :             dp->tdir_tag,dp->tdir_tag);
    3544                 :                                 /* the following knowingly leaks the 
    3545                 :                                    anonymous field structure */
    3546               0 :         if (!_TIFFMergeFields(tif,
    3547               0 :           _TIFFCreateAnonField(tif,
    3548               0 :             dp->tdir_tag,
    3549                 :             (TIFFDataType) dp->tdir_type),
    3550                 :           1)) {
    3551               0 :           TIFFWarningExt(tif->tif_clientdata,
    3552                 :               module,
    3553                 :               "Registering anonymous field with tag %d (0x%x) failed",
    3554               0 :               dp->tdir_tag,
    3555               0 :               dp->tdir_tag);
    3556               0 :           dp->tdir_tag=IGNORE;
    3557                 :         } else {
    3558               0 :           TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
    3559               0 :           assert(fii != FAILED_FII);
    3560                 :         }
    3561                 :       }
    3562                 :     }
    3563          148016 :     if (dp->tdir_tag!=IGNORE)
    3564                 :     {
    3565          127614 :       fip=tif->tif_fields[fii];
    3566          127614 :       if (fip->field_bit==FIELD_IGNORE)
    3567               0 :         dp->tdir_tag=IGNORE;
    3568                 :       else
    3569                 :       {
    3570          127614 :         switch (dp->tdir_tag)
    3571                 :         {
    3572                 :           case TIFFTAG_STRIPOFFSETS:
    3573                 :           case TIFFTAG_STRIPBYTECOUNTS:
    3574                 :           case TIFFTAG_TILEOFFSETS:
    3575                 :           case TIFFTAG_TILEBYTECOUNTS:
    3576           20412 :             TIFFSetFieldBit(tif,fip->field_bit);
    3577           20412 :             break;
    3578                 :           case TIFFTAG_IMAGEWIDTH:
    3579                 :           case TIFFTAG_IMAGELENGTH:
    3580                 :           case TIFFTAG_IMAGEDEPTH:
    3581                 :           case TIFFTAG_TILELENGTH:
    3582                 :           case TIFFTAG_TILEWIDTH:
    3583                 :           case TIFFTAG_TILEDEPTH:
    3584                 :           case TIFFTAG_PLANARCONFIG:
    3585                 :           case TIFFTAG_ROWSPERSTRIP:
    3586                 :           case TIFFTAG_EXTRASAMPLES:
    3587           43563 :             if (!TIFFFetchNormalTag(tif,dp,0))
    3588               0 :               goto bad;
    3589           43563 :             dp->tdir_tag=IGNORE;
    3590                 :             break;
    3591                 :         }
    3592                 :       }
    3593                 :     }
    3594                 :   }
    3595                 :   /*
    3596                 :    * XXX: OJPEG hack.
    3597                 :    * If a) compression is OJPEG, b) planarconfig tag says it's separate,
    3598                 :    * c) strip offsets/bytecounts tag are both present and
    3599                 :    * d) both contain exactly one value, then we consistently find
    3600                 :    * that the buggy implementation of the buggy compression scheme
    3601                 :    * matches contig planarconfig best. So we 'fix-up' the tag here
    3602                 :    */
    3603           10207 :   if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
    3604               1 :       (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
    3605                 :   {
    3606               0 :         if (!_TIFFFillStriles(tif))
    3607               0 :             goto bad;
    3608               0 :     dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
    3609               0 :     if ((dp!=0)&&(dp->tdir_count==1))
    3610                 :     {
    3611               0 :       dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
    3612                 :           TIFFTAG_STRIPBYTECOUNTS);
    3613               0 :       if ((dp!=0)&&(dp->tdir_count==1))
    3614                 :       {
    3615               0 :         tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
    3616               0 :         TIFFWarningExt(tif->tif_clientdata,module,
    3617                 :             "Planarconfig tag value assumed incorrect, "
    3618                 :             "assuming data is contig instead of chunky");
    3619                 :       }
    3620                 :     }
    3621                 :   }
    3622                 :   /*
    3623                 :    * Allocate directory structure and setup defaults.
    3624                 :    */
    3625           10206 :   if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
    3626                 :   {
    3627               0 :     MissingRequired(tif,"ImageLength");
    3628               0 :     goto bad;
    3629                 :   }
    3630                 :   /*
    3631                 :    * Setup appropriate structures (by strip or by tile)
    3632                 :    */
    3633           10206 :   if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
    3634            8049 :     tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);  
    3635            8049 :     tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
    3636            8049 :     tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
    3637            8049 :     tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
    3638            8049 :     tif->tif_flags &= ~TIFF_ISTILED;
    3639                 :   } else {
    3640            2157 :     tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
    3641            2157 :     tif->tif_flags |= TIFF_ISTILED;
    3642                 :   }
    3643           10206 :   if (!tif->tif_dir.td_nstrips) {
    3644               0 :     TIFFErrorExt(tif->tif_clientdata, module,
    3645                 :         "Cannot handle zero number of %s",
    3646               0 :         isTiled(tif) ? "tiles" : "strips");
    3647               0 :     goto bad;
    3648                 :   }
    3649           10206 :   tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
    3650           10206 :   if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
    3651             486 :     tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
    3652           10206 :   if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
    3653               0 :     if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
    3654               0 :         (isTiled(tif)==0) &&
    3655               0 :         (tif->tif_dir.td_nstrips==1)) {
    3656                 :       /*
    3657                 :        * XXX: OJPEG hack.
    3658                 :        * If a) compression is OJPEG, b) it's not a tiled TIFF,
    3659                 :        * and c) the number of strips is 1,
    3660                 :        * then we tolerate the absence of stripoffsets tag,
    3661                 :        * because, presumably, all required data is in the
    3662                 :        * JpegInterchangeFormat stream.
    3663                 :        */
    3664               0 :       TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
    3665                 :     } else {
    3666               0 :       MissingRequired(tif,
    3667               0 :         isTiled(tif) ? "TileOffsets" : "StripOffsets");
    3668               0 :       goto bad;
    3669                 :     }
    3670                 :   }
    3671                 :   /*
    3672                 :    * Second pass: extract other information.
    3673                 :    */
    3674          158222 :   for (di=0, dp=dir; di<dircount; di++, dp++)
    3675                 :   {
    3676          148016 :     switch (dp->tdir_tag)
    3677                 :     {
    3678                 :       case IGNORE:
    3679           63965 :         break;
    3680                 :       case TIFFTAG_MINSAMPLEVALUE:
    3681                 :       case TIFFTAG_MAXSAMPLEVALUE:
    3682                 :       case TIFFTAG_BITSPERSAMPLE:
    3683                 :       case TIFFTAG_DATATYPE:
    3684                 :       case TIFFTAG_SAMPLEFORMAT:
    3685                 :         /*
    3686                 :          * The MinSampleValue, MaxSampleValue, BitsPerSample
    3687                 :          * DataType and SampleFormat tags are supposed to be
    3688                 :          * written as one value/sample, but some vendors
    3689                 :          * incorrectly write one value only -- so we accept
    3690                 :          * that as well (yech). Other vendors write correct
    3691                 :          * value for NumberOfSamples, but incorrect one for
    3692                 :          * BitsPerSample and friends, and we will read this
    3693                 :          * too.
    3694                 :          */
    3695                 :         {
    3696                 :           uint16 value;
    3697                 :           enum TIFFReadDirEntryErr err;
    3698           20257 :           err=TIFFReadDirEntryShort(tif,dp,&value);
    3699           20257 :           if (err==TIFFReadDirEntryErrCount)
    3700            3672 :             err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
    3701           20257 :           if (err!=TIFFReadDirEntryErrOk)
    3702                 :           {
    3703               0 :             fip = TIFFFieldWithTag(tif,dp->tdir_tag);
    3704               0 :             TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
    3705               0 :             goto bad;
    3706                 :           }
    3707           20257 :           if (!TIFFSetField(tif,dp->tdir_tag,value))
    3708               0 :             goto bad;
    3709                 :         }
    3710           20257 :         break;
    3711                 :       case TIFFTAG_SMINSAMPLEVALUE:
    3712                 :       case TIFFTAG_SMAXSAMPLEVALUE:
    3713                 :         {
    3714                 : 
    3715                 :           double *data;
    3716                 :           enum TIFFReadDirEntryErr err;
    3717                 :           uint32 saved_flags;
    3718                 :           int m;
    3719               0 :           if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
    3720               0 :             err = TIFFReadDirEntryErrCount;
    3721                 :           else
    3722               0 :             err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    3723               0 :           if (err!=TIFFReadDirEntryErrOk)
    3724                 :           {
    3725               0 :             fip = TIFFFieldWithTag(tif,dp->tdir_tag);
    3726               0 :             TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
    3727               0 :             goto bad;
    3728                 :           }
    3729               0 :           saved_flags = tif->tif_flags;
    3730               0 :           tif->tif_flags |= TIFF_PERSAMPLE;
    3731               0 :           m = TIFFSetField(tif,dp->tdir_tag,data);
    3732               0 :           tif->tif_flags = saved_flags;
    3733               0 :           _TIFFfree(data);
    3734               0 :           if (!m)
    3735               0 :             goto bad;
    3736                 :         }
    3737               0 :         break;
    3738                 :       case TIFFTAG_STRIPOFFSETS:
    3739                 :       case TIFFTAG_TILEOFFSETS:
    3740                 : #if defined(DEFER_STRILE_LOAD)
    3741           10206 :                                 _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
    3742                 :                                              dp, sizeof(TIFFDirEntry) );
    3743                 : #else                          
    3744                 :         if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))  
    3745                 :           goto bad;
    3746                 : #endif                                
    3747           10206 :         break;
    3748                 :       case TIFFTAG_STRIPBYTECOUNTS:
    3749                 :       case TIFFTAG_TILEBYTECOUNTS:
    3750                 : #if defined(DEFER_STRILE_LOAD)
    3751           10206 :                                 _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
    3752                 :                                              dp, sizeof(TIFFDirEntry) );
    3753                 : #else                          
    3754                 :         if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))  
    3755                 :           goto bad;
    3756                 : #endif                                
    3757           10206 :         break;
    3758                 :       case TIFFTAG_COLORMAP:
    3759                 :       case TIFFTAG_TRANSFERFUNCTION:
    3760                 :         {
    3761                 :           enum TIFFReadDirEntryErr err;
    3762                 :           uint32 countpersample;
    3763                 :           uint32 countrequired;
    3764                 :           uint32 incrementpersample;
    3765             200 :           uint16* value=NULL;
    3766             200 :           countpersample=(1L<<tif->tif_dir.td_bitspersample);
    3767             200 :           if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
    3768                 :           {
    3769               0 :             countrequired=countpersample;
    3770               0 :             incrementpersample=0;
    3771                 :           }
    3772                 :           else
    3773                 :           {
    3774             200 :             countrequired=3*countpersample;
    3775             200 :             incrementpersample=countpersample;
    3776                 :           }
    3777             200 :           if (dp->tdir_count!=(uint64)countrequired)
    3778               0 :             err=TIFFReadDirEntryErrCount;
    3779                 :           else
    3780             200 :             err=TIFFReadDirEntryShortArray(tif,dp,&value);
    3781             200 :           if (err!=TIFFReadDirEntryErrOk)
    3782                 :                     {
    3783               0 :             fip = TIFFFieldWithTag(tif,dp->tdir_tag);
    3784               0 :             TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
    3785                 :                     }
    3786                 :           else
    3787                 :           {
    3788             200 :             TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
    3789             200 :             _TIFFfree(value);
    3790                 :           }
    3791                 :         }
    3792             200 :         break;
    3793                 : /* BEGIN REV 4.0 COMPATIBILITY */
    3794                 :       case TIFFTAG_OSUBFILETYPE:
    3795                 :         {
    3796                 :           uint16 valueo;
    3797                 :           uint32 value;
    3798               0 :           if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
    3799                 :           {
    3800               0 :             switch (valueo)
    3801                 :             {
    3802               0 :               case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
    3803               0 :               case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
    3804               0 :               default: value=0; break;
    3805                 :             }
    3806               0 :             if (value!=0)
    3807               0 :               TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
    3808                 :           }
    3809                 :         }
    3810               0 :         break;
    3811                 : /* END REV 4.0 COMPATIBILITY */
    3812                 :       default:
    3813           43182 :         (void) TIFFFetchNormalTag(tif, dp, TRUE);
    3814                 :         break;
    3815                 :     }
    3816                 :   }
    3817                 :   /*
    3818                 :    * OJPEG hack:
    3819                 :    * - If a) compression is OJPEG, and b) photometric tag is missing,
    3820                 :    * then we consistently find that photometric should be YCbCr
    3821                 :    * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
    3822                 :    * then we consistently find that the buggy implementation of the
    3823                 :    * buggy compression scheme matches photometric YCbCr instead.
    3824                 :    * - If a) compression is OJPEG, and b) bitspersample tag is missing,
    3825                 :    * then we consistently find bitspersample should be 8.
    3826                 :    * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
    3827                 :    * and c) photometric is RGB or YCbCr, then we consistently find
    3828                 :    * samplesperpixel should be 3
    3829                 :    * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
    3830                 :    * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
    3831                 :    * find samplesperpixel should be 3
    3832                 :    */
    3833           10206 :   if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
    3834                 :   {
    3835               1 :     if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
    3836                 :     {
    3837               0 :       TIFFWarningExt(tif->tif_clientdata, module,
    3838                 :           "Photometric tag is missing, assuming data is YCbCr");
    3839               0 :       if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
    3840               0 :         goto bad;
    3841                 :     }
    3842               1 :     else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
    3843                 :     {
    3844               0 :       tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
    3845               0 :       TIFFWarningExt(tif->tif_clientdata, module,
    3846                 :           "Photometric tag value assumed incorrect, "
    3847                 :           "assuming data is YCbCr instead of RGB");
    3848                 :     }
    3849               1 :     if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
    3850                 :     {
    3851               0 :       TIFFWarningExt(tif->tif_clientdata,module,
    3852                 :           "BitsPerSample tag is missing, assuming 8 bits per sample");
    3853               0 :       if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
    3854               0 :         goto bad;
    3855                 :     }
    3856               1 :     if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
    3857                 :     {
    3858               0 :       if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
    3859                 :       {
    3860               0 :         TIFFWarningExt(tif->tif_clientdata,module,
    3861                 :             "SamplesPerPixel tag is missing, "
    3862                 :             "assuming correct SamplesPerPixel value is 3");
    3863               0 :         if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
    3864               0 :           goto bad;
    3865                 :       }
    3866               0 :       if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
    3867                 :       {
    3868               0 :         TIFFWarningExt(tif->tif_clientdata,module,
    3869                 :             "SamplesPerPixel tag is missing, "
    3870                 :             "applying correct SamplesPerPixel value of 3");
    3871               0 :         if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
    3872               0 :           goto bad;
    3873                 :       }
    3874               0 :       else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
    3875               0 :          || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
    3876                 :       {
    3877                 :         /*
    3878                 :          * SamplesPerPixel tag is missing, but is not required
    3879                 :          * by spec.  Assume correct SamplesPerPixel value of 1.
    3880                 :          */
    3881               0 :         if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
    3882               0 :           goto bad;
    3883                 :       }
    3884                 :     }
    3885                 :   }
    3886                 :   /*
    3887                 :    * Verify Palette image has a Colormap.
    3888                 :    */
    3889           10400 :   if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
    3890             194 :       !TIFFFieldSet(tif, FIELD_COLORMAP)) {
    3891               0 :     if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
    3892               0 :       tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
    3893               0 :     else if (tif->tif_dir.td_bitspersample>=8)
    3894               0 :       tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
    3895                 :     else {
    3896               0 :       MissingRequired(tif, "Colormap");
    3897               0 :       goto bad;
    3898                 :     }
    3899                 :   }
    3900                 :   /*
    3901                 :    * OJPEG hack:
    3902                 :    * We do no further messing with strip/tile offsets/bytecounts in OJPEG
    3903                 :    * TIFFs
    3904                 :    */
    3905           10206 :   if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
    3906                 :   {
    3907                 :     /*
    3908                 :      * Attempt to deal with a missing StripByteCounts tag.
    3909                 :      */
    3910           10205 :     if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
    3911                 :       /*
    3912                 :        * Some manufacturers violate the spec by not giving
    3913                 :        * the size of the strips.  In this case, assume there
    3914                 :        * is one uncompressed strip of data.
    3915                 :        */
    3916               0 :       if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    3917               0 :           tif->tif_dir.td_nstrips > 1) ||
    3918               0 :           (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
    3919               0 :            tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
    3920               0 :           MissingRequired(tif, "StripByteCounts");
    3921               0 :           goto bad;
    3922                 :       }
    3923               0 :       TIFFWarningExt(tif->tif_clientdata, module,
    3924                 :         "TIFF directory is missing required "
    3925                 :         "\"StripByteCounts\" field, calculating from imagelength");
    3926               0 :       if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    3927               0 :           goto bad;
    3928                 :     /*
    3929                 :      * Assume we have wrong StripByteCount value (in case
    3930                 :      * of single strip) in following cases:
    3931                 :      *   - it is equal to zero along with StripOffset;
    3932                 :      *   - it is larger than file itself (in case of uncompressed
    3933                 :      *     image);
    3934                 :      *   - it is smaller than the size of the bytes per row
    3935                 :      *     multiplied on the number of rows.  The last case should
    3936                 :      *     not be checked in the case of writing new image,
    3937                 :      *     because we may do not know the exact strip size
    3938                 :      *     until the whole image will be written and directory
    3939                 :      *     dumped out.
    3940                 :      */
    3941                 :     #define BYTECOUNTLOOKSBAD \
    3942                 :         ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
    3943                 :           (tif->tif_dir.td_compression == COMPRESSION_NONE && \
    3944                 :            tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
    3945                 :           (tif->tif_mode == O_RDONLY && \
    3946                 :            tif->tif_dir.td_compression == COMPRESSION_NONE && \
    3947                 :            tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
    3948                 : 
    3949           65030 :     } else if (tif->tif_dir.td_nstrips == 1
    3950           10205 :                            && _TIFFFillStriles(tif)
    3951           16942 :          && tif->tif_dir.td_stripoffset[0] != 0
    3952           37883 :          && BYTECOUNTLOOKSBAD) {
    3953                 :       /*
    3954                 :        * XXX: Plexus (and others) sometimes give a value of
    3955                 :        * zero for a tag when they don't know what the
    3956                 :        * correct value is!  Try and handle the simple case
    3957                 :        * of estimating the size of a one strip image.
    3958                 :        */
    3959               1 :       TIFFWarningExt(tif->tif_clientdata, module,
    3960                 :           "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
    3961               1 :       if(EstimateStripByteCounts(tif, dir, dircount) < 0)
    3962               0 :           goto bad;
    3963                 : 
    3964                 : #if !defined(DEFER_STRILE_LOAD)
    3965                 :     } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
    3966                 :          && tif->tif_dir.td_nstrips > 2
    3967                 :          && tif->tif_dir.td_compression == COMPRESSION_NONE
    3968                 :          && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
    3969                 :          && tif->tif_dir.td_stripbytecount[0] != 0
    3970                 :          && tif->tif_dir.td_stripbytecount[1] != 0 ) {
    3971                 :       /*
    3972                 :        * XXX: Some vendors fill StripByteCount array with
    3973                 :        * absolutely wrong values (it can be equal to
    3974                 :        * StripOffset array, for example). Catch this case
    3975                 :        * here.
    3976                 :                          *
    3977                 :                          * We avoid this check if deferring strile loading
    3978                 :                          * as it would always force us to load the strip/tile
    3979                 :                          * information.
    3980                 :        */
    3981                 :       TIFFWarningExt(tif->tif_clientdata, module,
    3982                 :           "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
    3983                 :       if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    3984                 :           goto bad;
    3985                 : #endif /* !defined(DEFER_STRILE_LOAD) */                        
    3986                 :     }
    3987                 :   }
    3988           10206 :   if (dir)
    3989                 :   {
    3990           10206 :     _TIFFfree(dir);
    3991           10206 :     dir=NULL;
    3992                 :   }
    3993           10206 :   if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
    3994                 :   {
    3995           10206 :     if (tif->tif_dir.td_bitspersample>=16)
    3996            4286 :       tif->tif_dir.td_maxsamplevalue=0xFFFF;
    3997                 :     else
    3998            5920 :       tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
    3999                 :   }
    4000                 :   /*
    4001                 :    * XXX: We can optimize checking for the strip bounds using the sorted
    4002                 :    * bytecounts array. See also comments for TIFFAppendToStrip()
    4003                 :    * function in tif_write.c.
    4004                 :    */
    4005                 : #if !defined(DEFER_STRILE_LOAD)        
    4006                 :   if (tif->tif_dir.td_nstrips > 1) {
    4007                 :     uint32 strip;
    4008                 : 
    4009                 :     tif->tif_dir.td_stripbytecountsorted = 1;
    4010                 :     for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
    4011                 :       if (tif->tif_dir.td_stripoffset[strip - 1] >
    4012                 :           tif->tif_dir.td_stripoffset[strip]) {
    4013                 :         tif->tif_dir.td_stripbytecountsorted = 0;
    4014                 :         break;
    4015                 :       }
    4016                 :     }
    4017                 :   }
    4018                 : #endif /* !defined(DEFER_STRILE_LOAD) */
    4019                 :         
    4020                 :   /*
    4021                 :    * An opportunity for compression mode dependent tag fixup
    4022                 :    */
    4023           10206 :   (*tif->tif_fixuptags)(tif);
    4024                 : 
    4025                 :   /*
    4026                 :    * Some manufacturers make life difficult by writing
    4027                 :    * large amounts of uncompressed data as a single strip.
    4028                 :    * This is contrary to the recommendations of the spec.
    4029                 :    * The following makes an attempt at breaking such images
    4030                 :    * into strips closer to the recommended 8k bytes.  A
    4031                 :    * side effect, however, is that the RowsPerStrip tag
    4032                 :    * value may be changed.
    4033                 :    */
    4034           36402 :   if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
    4035            9720 :       (tif->tif_dir.td_nstrips==1)&&
    4036            8462 :       (tif->tif_dir.td_compression==COMPRESSION_NONE)&&  
    4037            8014 :       ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
    4038                 :     {
    4039            3417 :         if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
    4040               0 :             return 0;
    4041            3417 :     ChopUpSingleUncompressedStrip(tif);
    4042                 :     }
    4043                 : 
    4044                 :         /*
    4045                 :          * Clear the dirty directory flag. 
    4046                 :          */
    4047           10206 :   tif->tif_flags &= ~TIFF_DIRTYDIRECT;
    4048           10206 :   tif->tif_flags &= ~TIFF_DIRTYSTRIP;
    4049                 : 
    4050                 :   /*
    4051                 :    * Reinitialize i/o since we are starting on a new directory.
    4052                 :    */
    4053           10206 :   tif->tif_row = (uint32) -1;
    4054           10206 :   tif->tif_curstrip = (uint32) -1;
    4055           10206 :   tif->tif_col = (uint32) -1;
    4056           10206 :   tif->tif_curtile = (uint32) -1;
    4057           10206 :   tif->tif_tilesize = (tmsize_t) -1;
    4058                 : 
    4059           10206 :   tif->tif_scanlinesize = TIFFScanlineSize(tif);
    4060           10206 :   if (!tif->tif_scanlinesize) {
    4061               0 :     TIFFErrorExt(tif->tif_clientdata, module,
    4062                 :         "Cannot handle zero scanline size");
    4063               0 :     return (0);
    4064                 :   }
    4065                 : 
    4066           10206 :   if (isTiled(tif)) {
    4067            2157 :     tif->tif_tilesize = TIFFTileSize(tif);
    4068            2157 :     if (!tif->tif_tilesize) {
    4069               0 :       TIFFErrorExt(tif->tif_clientdata, module,
    4070                 :            "Cannot handle zero tile size");
    4071               0 :       return (0);
    4072                 :     }
    4073                 :   } else {
    4074            8049 :     if (!TIFFStripSize(tif)) {
    4075               0 :       TIFFErrorExt(tif->tif_clientdata, module,
    4076                 :           "Cannot handle zero strip size");
    4077               0 :       return (0);
    4078                 :     }
    4079                 :   }
    4080           10206 :   return (1);
    4081                 : bad:
    4082               0 :   if (dir)
    4083               0 :     _TIFFfree(dir);
    4084               0 :   return (0);
    4085                 : }
    4086                 : 
    4087                 : static void
    4088           10206 : TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
    4089                 : {
    4090                 :   static const char module[] = "TIFFReadDirectoryCheckOrder";
    4091                 :   uint16 m;
    4092                 :   uint16 n;
    4093                 :   TIFFDirEntry* o;
    4094           10206 :   m=0;
    4095          158222 :   for (n=0, o=dir; n<dircount; n++, o++)
    4096                 :   {
    4097          148016 :     if (o->tdir_tag<m)
    4098                 :     {
    4099               0 :       TIFFWarningExt(tif->tif_clientdata,module,
    4100                 :           "Invalid TIFF directory; tags are not sorted in ascending order");
    4101               0 :       break;
    4102                 :     }
    4103          148016 :     m=o->tdir_tag+1;
    4104                 :   }
    4105           10206 : }
    4106                 : 
    4107                 : static TIFFDirEntry*
    4108           20412 : TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
    4109                 : {
    4110                 :   TIFFDirEntry* m;
    4111                 :   uint16 n;
    4112                 :   (void) tif;
    4113          114561 :   for (m=dir, n=0; n<dircount; m++, n++)
    4114                 :   {
    4115          114551 :     if (m->tdir_tag==tagid)
    4116           20402 :       return(m);
    4117                 :   }
    4118              10 :   return(0);
    4119                 : }
    4120                 : 
    4121                 : static void
    4122          224557 : TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
    4123                 : {
    4124                 :   int32 ma,mb,mc;
    4125          224557 :   ma=-1;
    4126          224557 :   mc=(int32)tif->tif_nfields;
    4127                 :   while (1)
    4128                 :   {
    4129         1426622 :     if (ma+1==mc)
    4130                 :     {
    4131               0 :       *fii = FAILED_FII;
    4132               0 :       return;
    4133                 :     }
    4134         1426622 :     mb=(ma+mc)/2;
    4135         1426622 :     if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
    4136          224557 :       break;
    4137         1202065 :     if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
    4138          428584 :       ma=mb;
    4139                 :     else
    4140          773481 :       mc=mb;
    4141         1202065 :   }
    4142                 :   while (1)
    4143                 :   {
    4144          224557 :     if (mb==0)
    4145            4332 :       break;
    4146          220225 :     if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
    4147          220225 :       break;
    4148               0 :     mb--;
    4149               0 :   }
    4150          224557 :   *fii=mb;
    4151                 : }
    4152                 : 
    4153                 : /*
    4154                 :  * Read custom directory from the arbitarry offset.
    4155                 :  * The code is very similar to TIFFReadDirectory().
    4156                 :  */
    4157                 : int
    4158               0 : TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
    4159                 :       const TIFFFieldArray* infoarray)
    4160                 : {
    4161                 :   static const char module[] = "TIFFReadCustomDirectory";
    4162                 :   TIFFDirEntry* dir;
    4163                 :   uint16 dircount;
    4164                 :   TIFFDirEntry* dp;
    4165                 :   uint16 di;
    4166                 :   const TIFFField* fip;
    4167                 :   uint32 fii;
    4168               0 :   _TIFFSetupFields(tif, infoarray);
    4169               0 :   dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
    4170               0 :   if (!dircount)
    4171                 :   {
    4172               0 :     TIFFErrorExt(tif->tif_clientdata,module,
    4173                 :         "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
    4174               0 :     return 0;
    4175                 :   }
    4176               0 :   TIFFFreeDirectory(tif);
    4177               0 :   _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
    4178               0 :   TIFFReadDirectoryCheckOrder(tif,dir,dircount);
    4179               0 :   for (di=0, dp=dir; di<dircount; di++, dp++)
    4180                 :   {
    4181               0 :     TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
    4182               0 :     if (fii == FAILED_FII)
    4183                 :     {
    4184               0 :       TIFFWarningExt(tif->tif_clientdata, module,
    4185                 :           "Unknown field with tag %d (0x%x) encountered",
    4186               0 :           dp->tdir_tag, dp->tdir_tag);
    4187               0 :       if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
    4188               0 :             dp->tdir_tag,
    4189                 :             (TIFFDataType) dp->tdir_type),
    4190                 :                1)) {
    4191               0 :         TIFFWarningExt(tif->tif_clientdata, module,
    4192                 :             "Registering anonymous field with tag %d (0x%x) failed",
    4193               0 :             dp->tdir_tag, dp->tdir_tag);
    4194               0 :         dp->tdir_tag=IGNORE;
    4195                 :       } else {
    4196               0 :         TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
    4197               0 :         assert( fii != FAILED_FII );
    4198                 :       }
    4199                 :     }
    4200               0 :     if (dp->tdir_tag!=IGNORE)
    4201                 :     {
    4202               0 :       fip=tif->tif_fields[fii];
    4203               0 :       if (fip->field_bit==FIELD_IGNORE)
    4204               0 :         dp->tdir_tag=IGNORE;
    4205                 :       else
    4206                 :       {
    4207                 :         /* check data type */
    4208               0 :         while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
    4209                 :         {
    4210               0 :           fii++;
    4211               0 :           if ((fii==tif->tif_nfields)||
    4212               0 :               (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
    4213                 :           {
    4214               0 :             fii=0xFFFF;
    4215               0 :             break;
    4216                 :           }
    4217               0 :           fip=tif->tif_fields[fii];
    4218                 :         }
    4219               0 :         if (fii==0xFFFF)
    4220                 :         {
    4221               0 :           TIFFWarningExt(tif->tif_clientdata, module,
    4222                 :               "Wrong data type %d for \"%s\"; tag ignored",
    4223               0 :               dp->tdir_type,fip->field_name);
    4224               0 :           dp->tdir_tag=IGNORE;
    4225                 :         }
    4226                 :         else
    4227                 :         {
    4228                 :           /* check count if known in advance */
    4229               0 :           if ((fip->field_readcount!=TIFF_VARIABLE)&&
    4230               0 :               (fip->field_readcount!=TIFF_VARIABLE2))
    4231                 :           {
    4232                 :             uint32 expected;
    4233               0 :             if (fip->field_readcount==TIFF_SPP)
    4234               0 :               expected=(uint32)tif->tif_dir.td_samplesperpixel;
    4235                 :             else
    4236               0 :               expected=(uint32)fip->field_readcount;
    4237               0 :             if (!CheckDirCount(tif,dp,expected))
    4238               0 :               dp->tdir_tag=IGNORE;
    4239                 :           }
    4240                 :         }
    4241                 :       }
    4242               0 :       switch (dp->tdir_tag)
    4243                 :       {
    4244                 :         case IGNORE:
    4245               0 :           break;
    4246                 :         case EXIFTAG_SUBJECTDISTANCE:
    4247               0 :           (void) TIFFFetchSubjectDistance(tif,dp);
    4248               0 :           break;
    4249                 :         default:
    4250               0 :           (void) TIFFFetchNormalTag(tif, dp, TRUE);
    4251                 :           break;
    4252                 :       }
    4253                 :     }
    4254                 :   }
    4255               0 :   if (dir)
    4256               0 :     _TIFFfree(dir);
    4257               0 :   return 1;
    4258                 : }
    4259                 : 
    4260                 : /*
    4261                 :  * EXIF is important special case of custom IFD, so we have a special
    4262                 :  * function to read it.
    4263                 :  */
    4264                 : int
    4265               0 : TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
    4266                 : {
    4267                 :   const TIFFFieldArray* exifFieldArray;
    4268               0 :   exifFieldArray = _TIFFGetExifFields();
    4269               0 :   return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);  
    4270                 : }
    4271                 : 
    4272                 : static int
    4273               1 : EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
    4274                 : {
    4275                 :   static const char module[] = "EstimateStripByteCounts";
    4276                 : 
    4277                 :   TIFFDirEntry *dp;
    4278               1 :   TIFFDirectory *td = &tif->tif_dir;
    4279                 :   uint32 strip;
    4280                 : 
    4281               1 :     _TIFFFillStriles( tif );
    4282                 : 
    4283               1 :   if (td->td_stripbytecount)
    4284               1 :     _TIFFfree(td->td_stripbytecount);
    4285               1 :   td->td_stripbytecount = (uint64*)
    4286               1 :       _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
    4287                 :     "for \"StripByteCounts\" array");
    4288               1 :         if( td->td_stripbytecount == NULL )
    4289               0 :             return -1;
    4290                 : 
    4291               1 :   if (td->td_compression != COMPRESSION_NONE) {
    4292                 :     uint64 space;
    4293                 :     uint64 filesize;
    4294                 :     uint16 n;
    4295               0 :     filesize = TIFFGetFileSize(tif);
    4296               0 :     if (!(tif->tif_flags&TIFF_BIGTIFF))
    4297               0 :       space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
    4298                 :     else
    4299               0 :       space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
    4300                 :     /* calculate amount of space used by indirect values */
    4301               0 :     for (dp = dir, n = dircount; n > 0; n--, dp++)
    4302                 :     {
    4303               0 :       uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
    4304                 :       uint64 datasize;
    4305               0 :       typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
    4306               0 :       if (typewidth == 0) {
    4307               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4308                 :             "Cannot determine size of unknown tag type %d",
    4309               0 :             dp->tdir_type);
    4310               0 :         return -1;
    4311                 :       }
    4312               0 :       datasize=(uint64)typewidth*dp->tdir_count;
    4313               0 :       if (!(tif->tif_flags&TIFF_BIGTIFF))
    4314                 :       {
    4315               0 :         if (datasize<=4)
    4316               0 :           datasize=0;
    4317                 :       }
    4318                 :       else
    4319                 :       {
    4320               0 :         if (datasize<=8)
    4321               0 :           datasize=0;
    4322                 :       }
    4323               0 :       space+=datasize;
    4324                 :     }
    4325               0 :     space = filesize - space;
    4326               0 :     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
    4327               0 :       space /= td->td_samplesperpixel;
    4328               0 :     for (strip = 0; strip < td->td_nstrips; strip++)
    4329               0 :       td->td_stripbytecount[strip] = space;
    4330                 :     /*
    4331                 :      * This gross hack handles the case were the offset to
    4332                 :      * the last strip is past the place where we think the strip
    4333                 :      * should begin.  Since a strip of data must be contiguous,
    4334                 :      * it's safe to assume that we've overestimated the amount
    4335                 :      * of data in the strip and trim this number back accordingly.
    4336                 :      */
    4337               0 :     strip--;
    4338               0 :     if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
    4339               0 :       td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
    4340               1 :   } else if (isTiled(tif)) {
    4341               0 :     uint64 bytespertile = TIFFTileSize64(tif);
    4342                 : 
    4343               0 :     for (strip = 0; strip < td->td_nstrips; strip++)
    4344               0 :         td->td_stripbytecount[strip] = bytespertile;
    4345                 :   } else {
    4346               1 :     uint64 rowbytes = TIFFScanlineSize64(tif);
    4347               1 :     uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
    4348               2 :     for (strip = 0; strip < td->td_nstrips; strip++)
    4349               1 :       td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
    4350                 :   }
    4351               1 :   TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
    4352               1 :   if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
    4353               0 :     td->td_rowsperstrip = td->td_imagelength;
    4354               1 :   return 1;
    4355                 : }
    4356                 : 
    4357                 : static void
    4358               0 : MissingRequired(TIFF* tif, const char* tagname)
    4359                 : {
    4360                 :   static const char module[] = "MissingRequired";
    4361                 : 
    4362               0 :   TIFFErrorExt(tif->tif_clientdata, module,
    4363                 :       "TIFF directory is missing required \"%s\" field",
    4364                 :       tagname);
    4365               0 : }
    4366                 : 
    4367                 : /*
    4368                 :  * Check the directory offset against the list of already seen directory
    4369                 :  * offsets. This is a trick to prevent IFD looping. The one can create TIFF
    4370                 :  * file with looped directory pointers. We will maintain a list of already
    4371                 :  * seen directories and check every IFD offset against that list.
    4372                 :  */
    4373                 : static int
    4374           10303 : TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
    4375                 : {
    4376                 :   uint16 n;
    4377                 : 
    4378           10303 :   if (diroff == 0)      /* no more directories */
    4379              97 :     return 0;
    4380                 : 
    4381           10604 :   for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
    4382             398 :     if (tif->tif_dirlist[n] == diroff)
    4383               0 :       return 0;
    4384                 :   }
    4385                 : 
    4386           10206 :   tif->tif_dirnumber++;
    4387                 : 
    4388           10206 :   if (tif->tif_dirnumber > tif->tif_dirlistsize) {
    4389                 :     uint64* new_dirlist;
    4390                 : 
    4391                 :     /*
    4392                 :      * XXX: Reduce memory allocation granularity of the dirlist
    4393                 :      * array.
    4394                 :      */
    4395            8014 :     new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
    4396            8014 :         tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
    4397            8014 :     if (!new_dirlist)
    4398               0 :       return 0;
    4399            8014 :     tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
    4400            8014 :     tif->tif_dirlist = new_dirlist;
    4401                 :   }
    4402                 : 
    4403           10206 :   tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
    4404                 : 
    4405           10206 :   return 1;
    4406                 : }
    4407                 : 
    4408                 : /*
    4409                 :  * Check the count field of a directory entry against a known value.  The
    4410                 :  * caller is expected to skip/ignore the tag if there is a mismatch.
    4411                 :  */
    4412                 : static int
    4413               0 : CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
    4414                 : {
    4415               0 :   if ((uint64)count > dir->tdir_count) {
    4416               0 :     const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    4417               0 :     TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
    4418                 :   "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
    4419                 :         fip ? fip->field_name : "unknown tagname",
    4420                 :         dir->tdir_count, count);
    4421               0 :     return (0);
    4422               0 :   } else if ((uint64)count < dir->tdir_count) {
    4423               0 :     const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    4424               0 :     TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
    4425                 :   "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
    4426                 :         fip ? fip->field_name : "unknown tagname",
    4427                 :         dir->tdir_count, count);
    4428               0 :     dir->tdir_count = count;
    4429               0 :     return (1);
    4430                 :   }
    4431               0 :   return (1);
    4432                 : }
    4433                 : 
    4434                 : /*
    4435                 :  * Read IFD structure from the specified offset. If the pointer to
    4436                 :  * nextdiroff variable has been specified, read it too. Function returns a
    4437                 :  * number of fields in the directory or 0 if failed.
    4438                 :  */
    4439                 : static uint16
    4440           10206 : TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
    4441                 :                    uint64 *nextdiroff)
    4442                 : {
    4443                 :   static const char module[] = "TIFFFetchDirectory";
    4444                 : 
    4445                 :   void* origdir;
    4446                 :   uint16 dircount16;
    4447                 :   uint32 dirsize;
    4448                 :   TIFFDirEntry* dir;
    4449                 :   uint8* ma;
    4450                 :   TIFFDirEntry* mb;
    4451                 :   uint16 n;
    4452                 : 
    4453           10206 :   assert(pdir);
    4454                 : 
    4455           10206 :   tif->tif_diroff = diroff;
    4456           10206 :   if (nextdiroff)
    4457           10206 :     *nextdiroff = 0;
    4458           10206 :   if (!isMapped(tif)) {
    4459           10206 :     if (!SeekOK(tif, tif->tif_diroff)) {
    4460               0 :       TIFFErrorExt(tif->tif_clientdata, module,
    4461                 :         "%s: Seek error accessing TIFF directory",
    4462                 :         tif->tif_name);
    4463               0 :       return 0;
    4464                 :     }
    4465           10206 :     if (!(tif->tif_flags&TIFF_BIGTIFF))
    4466                 :     {
    4467           10107 :       if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
    4468               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4469                 :             "%s: Can not read TIFF directory count",
    4470                 :             tif->tif_name);
    4471               0 :         return 0;
    4472                 :       }
    4473           10107 :       if (tif->tif_flags & TIFF_SWAB)
    4474             372 :         TIFFSwabShort(&dircount16);
    4475           10107 :       if (dircount16>4096)
    4476                 :       {
    4477               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4478                 :             "Sanity check on directory count failed, this is probably not a valid IFD offset");
    4479               0 :         return 0;
    4480                 :       }
    4481           10107 :       dirsize = 12;
    4482                 :     } else {
    4483                 :       uint64 dircount64;
    4484              99 :       if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
    4485               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4486                 :           "%s: Can not read TIFF directory count",
    4487                 :           tif->tif_name);
    4488               0 :         return 0;
    4489                 :       }
    4490              99 :       if (tif->tif_flags & TIFF_SWAB)
    4491              12 :         TIFFSwabLong8(&dircount64);
    4492              99 :       if (dircount64>4096)
    4493                 :       {
    4494               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4495                 :             "Sanity check on directory count failed, this is probably not a valid IFD offset");
    4496               0 :         return 0;
    4497                 :       }
    4498              99 :       dircount16 = (uint16)dircount64;
    4499              99 :       dirsize = 20;
    4500                 :     }
    4501           10206 :     origdir = _TIFFCheckMalloc(tif, dircount16,
    4502                 :         dirsize, "to read TIFF directory");
    4503           10206 :     if (origdir == NULL)
    4504               0 :       return 0;
    4505           10206 :     if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
    4506               0 :       TIFFErrorExt(tif->tif_clientdata, module,
    4507                 :         "%.100s: Can not read TIFF directory",
    4508                 :         tif->tif_name);
    4509               0 :       _TIFFfree(origdir);
    4510               0 :       return 0;
    4511                 :     }
    4512                 :     /*
    4513                 :      * Read offset to next directory for sequential scans if
    4514                 :      * needed.
    4515                 :      */
    4516           10206 :     if (nextdiroff)
    4517                 :     {
    4518           10206 :       if (!(tif->tif_flags&TIFF_BIGTIFF))
    4519                 :       {
    4520                 :         uint32 nextdiroff32;
    4521           10107 :         if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
    4522               2 :           nextdiroff32 = 0;
    4523           10107 :         if (tif->tif_flags&TIFF_SWAB)
    4524             372 :           TIFFSwabLong(&nextdiroff32);
    4525           10107 :         *nextdiroff=nextdiroff32;
    4526                 :       } else {
    4527              99 :         if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
    4528               0 :           *nextdiroff = 0;
    4529              99 :         if (tif->tif_flags&TIFF_SWAB)
    4530              12 :           TIFFSwabLong8(nextdiroff);
    4531                 :       }
    4532                 :     }
    4533                 :   } else {
    4534                 :     tmsize_t m;
    4535               0 :     tmsize_t off = (tmsize_t) tif->tif_diroff;
    4536               0 :     if ((uint64)off!=tif->tif_diroff)
    4537                 :     {
    4538               0 :       TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
    4539               0 :       return(0);
    4540                 :     }
    4541                 : 
    4542                 :     /*
    4543                 :      * Check for integer overflow when validating the dir_off,
    4544                 :      * otherwise a very high offset may cause an OOB read and
    4545                 :      * crash the client. Make two comparisons instead of
    4546                 :      *
    4547                 :      *  off + sizeof(uint16) > tif->tif_size
    4548                 :      *
    4549                 :      * to avoid overflow.
    4550                 :      */
    4551               0 :     if (!(tif->tif_flags&TIFF_BIGTIFF))
    4552                 :     {
    4553               0 :       m=off+sizeof(uint16);
    4554               0 :       if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
    4555               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4556                 :           "Can not read TIFF directory count");
    4557               0 :         return 0;
    4558                 :       } else {
    4559               0 :         _TIFFmemcpy(&dircount16, tif->tif_base + off,
    4560                 :               sizeof(uint16));
    4561                 :       }
    4562               0 :       off += sizeof (uint16);
    4563               0 :       if (tif->tif_flags & TIFF_SWAB)
    4564               0 :         TIFFSwabShort(&dircount16);
    4565               0 :       if (dircount16>4096)
    4566                 :       {
    4567               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4568                 :             "Sanity check on directory count failed, this is probably not a valid IFD offset");
    4569               0 :         return 0;
    4570                 :       }
    4571               0 :       dirsize = 12;
    4572                 :     }
    4573                 :     else
    4574                 :     {
    4575                 :       tmsize_t m;
    4576                 :       uint64 dircount64;
    4577               0 :       m=off+sizeof(uint64);
    4578               0 :       if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
    4579               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4580                 :           "Can not read TIFF directory count");
    4581               0 :         return 0;
    4582                 :       } else {
    4583               0 :         _TIFFmemcpy(&dircount64, tif->tif_base + off,
    4584                 :               sizeof(uint64));
    4585                 :       }
    4586               0 :       off += sizeof (uint64);
    4587               0 :       if (tif->tif_flags & TIFF_SWAB)
    4588               0 :         TIFFSwabLong8(&dircount64);
    4589               0 :       if (dircount64>4096)
    4590                 :       {
    4591               0 :         TIFFErrorExt(tif->tif_clientdata, module,
    4592                 :             "Sanity check on directory count failed, this is probably not a valid IFD offset");
    4593               0 :         return 0;
    4594                 :       }
    4595               0 :       dircount16 = (uint16)dircount64;
    4596               0 :       dirsize = 20;
    4597                 :     }
    4598               0 :     if (dircount16 == 0 )
    4599                 :     {
    4600               0 :       TIFFErrorExt(tif->tif_clientdata, module,
    4601                 :                    "Sanity check on directory count failed, zero tag directories not supported");
    4602               0 :       return 0;
    4603                 :     }
    4604               0 :     origdir = _TIFFCheckMalloc(tif, dircount16,
    4605                 :             dirsize,
    4606                 :             "to read TIFF directory");
    4607               0 :     if (origdir == NULL)
    4608               0 :       return 0;
    4609               0 :     m=off+dircount16*dirsize;
    4610               0 :     if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
    4611               0 :       TIFFErrorExt(tif->tif_clientdata, module,
    4612                 :              "Can not read TIFF directory");
    4613               0 :       _TIFFfree(origdir);
    4614               0 :       return 0;
    4615                 :     } else {
    4616               0 :       _TIFFmemcpy(origdir, tif->tif_base + off,
    4617               0 :             dircount16 * dirsize);
    4618                 :     }
    4619               0 :     if (nextdiroff) {
    4620               0 :       off += dircount16 * dirsize;
    4621               0 :       if (!(tif->tif_flags&TIFF_BIGTIFF))
    4622                 :       {
    4623                 :         uint32 nextdiroff32;
    4624               0 :         m=off+sizeof(uint32);
    4625               0 :         if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
    4626               0 :           nextdiroff32 = 0;
    4627                 :         else
    4628               0 :           _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
    4629                 :                 sizeof (uint32));
    4630               0 :         if (tif->tif_flags&TIFF_SWAB)
    4631               0 :           TIFFSwabLong(&nextdiroff32);
    4632               0 :         *nextdiroff = nextdiroff32;
    4633                 :       }
    4634                 :       else
    4635                 :       {
    4636               0 :         m=off+sizeof(uint64);
    4637               0 :         if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
    4638               0 :           *nextdiroff = 0;
    4639                 :         else
    4640               0 :           _TIFFmemcpy(nextdiroff, tif->tif_base + off,
    4641                 :                 sizeof (uint64));
    4642               0 :         if (tif->tif_flags&TIFF_SWAB)
    4643               0 :           TIFFSwabLong8(nextdiroff);
    4644                 :       }
    4645                 :     }
    4646                 :   }
    4647           10206 :   dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
    4648                 :             sizeof(TIFFDirEntry),
    4649                 :             "to read TIFF directory");
    4650           10206 :   if (dir==0)
    4651                 :   {
    4652               0 :     _TIFFfree(origdir);
    4653               0 :     return 0;
    4654                 :   }
    4655           10206 :   ma=(uint8*)origdir;
    4656           10206 :   mb=dir;
    4657          158222 :   for (n=0; n<dircount16; n++)
    4658                 :   {
    4659          148016 :     if (tif->tif_flags&TIFF_SWAB)
    4660            5144 :       TIFFSwabShort((uint16*)ma);
    4661          148016 :     mb->tdir_tag=*(uint16*)ma;
    4662          148016 :     ma+=sizeof(uint16);
    4663          148016 :     if (tif->tif_flags&TIFF_SWAB)
    4664            5144 :       TIFFSwabShort((uint16*)ma);
    4665          148016 :     mb->tdir_type=*(uint16*)ma;
    4666          148016 :     ma+=sizeof(uint16);
    4667          148016 :     if (!(tif->tif_flags&TIFF_BIGTIFF))
    4668                 :     {
    4669          146806 :       if (tif->tif_flags&TIFF_SWAB)
    4670            5012 :         TIFFSwabLong((uint32*)ma);
    4671          146806 :       mb->tdir_count=(uint64)(*(uint32*)ma);
    4672          146806 :       ma+=sizeof(uint32);
    4673          146806 :       *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
    4674          146806 :       ma+=sizeof(uint32);
    4675                 :     }
    4676                 :     else
    4677                 :     {
    4678            1210 :       if (tif->tif_flags&TIFF_SWAB)
    4679             132 :         TIFFSwabLong8((uint64*)ma);
    4680            1210 :                         mb->tdir_count=TIFFReadUInt64(ma);
    4681            1210 :       ma+=sizeof(uint64);
    4682            1210 :       mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
    4683            1210 :       ma+=sizeof(uint64);
    4684                 :     }
    4685          148016 :     mb++;
    4686                 :   }
    4687           10206 :   _TIFFfree(origdir);
    4688           10206 :   *pdir = dir;
    4689           10206 :   return dircount16;
    4690                 : }
    4691                 : 
    4692                 : /*
    4693                 :  * Fetch a tag that is not handled by special case code.
    4694                 :  */
    4695                 : static int
    4696           96943 : TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
    4697                 : {
    4698                 :   static const char module[] = "TIFFFetchNormalTag";
    4699                 :   enum TIFFReadDirEntryErr err;
    4700                 :   uint32 fii;
    4701           96943 :   const TIFFField* fip = NULL;
    4702           96943 :   TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
    4703           96943 :         if( fii == FAILED_FII )
    4704                 :         {
    4705               0 :             TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
    4706                 :                          "No definition found for tag %d",
    4707               0 :                          dp->tdir_tag);
    4708               0 :             return 0;
    4709                 :         }
    4710           96943 :   fip=tif->tif_fields[fii];
    4711           96943 :   assert(fip->set_field_type!=TIFF_SETGET_OTHER);  /* if so, we shouldn't arrive here but deal with this in specialized code */
    4712           96943 :   assert(fip->set_field_type!=TIFF_SETGET_INT);    /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
    4713           96943 :   err=TIFFReadDirEntryErrOk;
    4714           96943 :   switch (fip->set_field_type)
    4715                 :   {
    4716                 :     case TIFF_SETGET_UNDEFINED:
    4717               0 :       break;
    4718                 :     case TIFF_SETGET_ASCII:
    4719                 :       {
    4720                 :         uint8* data;
    4721            7297 :         assert(fip->field_passcount==0);
    4722            7297 :         err=TIFFReadDirEntryByteArray(tif,dp,&data);
    4723            7297 :         if (err==TIFFReadDirEntryErrOk)
    4724                 :         {
    4725                 :           uint8* ma;
    4726                 :           uint32 mb;
    4727                 :           int n;
    4728            7295 :           ma=data;
    4729            7295 :           mb=0;
    4730          212319 :           while (mb<(uint32)dp->tdir_count)
    4731                 :           {
    4732          205022 :             if (*ma==0)
    4733            7293 :               break;
    4734          197729 :             ma++;
    4735          197729 :             mb++;
    4736                 :           }
    4737            7295 :           if (mb+1<(uint32)dp->tdir_count)
    4738               0 :             TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip->field_name);
    4739            7295 :           else if (mb+1>(uint32)dp->tdir_count)
    4740                 :           {
    4741                 :             uint8* o;
    4742               2 :             TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
    4743               2 :             if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
    4744               0 :               o=NULL;
    4745                 :             else
    4746               2 :               o=_TIFFmalloc((uint32)dp->tdir_count+1);
    4747               2 :             if (o==NULL)
    4748                 :             {
    4749               0 :               if (data!=NULL)
    4750               0 :                 _TIFFfree(data);
    4751               0 :               return(0);
    4752                 :             }
    4753               2 :             _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
    4754               2 :             o[(uint32)dp->tdir_count]=0;
    4755               2 :             if (data!=0)
    4756               2 :               _TIFFfree(data);
    4757               2 :             data=o;
    4758                 :           }
    4759            7295 :           n=TIFFSetField(tif,dp->tdir_tag,data);
    4760            7295 :           if (data!=0)
    4761            7295 :             _TIFFfree(data);
    4762            7295 :           if (!n)
    4763               0 :             return(0);
    4764                 :         }
    4765                 :       }
    4766            7297 :       break;
    4767                 :     case TIFF_SETGET_UINT8:
    4768                 :       {
    4769               0 :         uint8 data=0;
    4770               0 :         assert(fip->field_readcount==1);
    4771               0 :         assert(fip->field_passcount==0);
    4772               0 :         err=TIFFReadDirEntryByte(tif,dp,&data);
    4773               0 :         if (err==TIFFReadDirEntryErrOk)
    4774                 :         {
    4775               0 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4776               0 :             return(0);
    4777                 :         }
    4778                 :       }
    4779               0 :       break;
    4780                 :     case TIFF_SETGET_UINT16:
    4781                 :       {
    4782                 :         uint16 data;
    4783           31180 :         assert(fip->field_readcount==1);
    4784           31180 :         assert(fip->field_passcount==0);
    4785           31180 :         err=TIFFReadDirEntryShort(tif,dp,&data);
    4786           31180 :         if (err==TIFFReadDirEntryErrOk)
    4787                 :         {
    4788           31180 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4789               0 :             return(0);
    4790                 :         }
    4791                 :       }
    4792           31180 :       break;
    4793                 :     case TIFF_SETGET_UINT32:
    4794                 :       {
    4795                 :         uint32 data;
    4796           34902 :         assert(fip->field_readcount==1);
    4797           34902 :         assert(fip->field_passcount==0);
    4798           34902 :         err=TIFFReadDirEntryLong(tif,dp,&data);
    4799           34902 :         if (err==TIFFReadDirEntryErrOk)
    4800                 :         {
    4801           34902 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4802               0 :             return(0);
    4803                 :         }
    4804                 :       }
    4805           34902 :       break;
    4806                 :     case TIFF_SETGET_UINT64:
    4807                 :       {
    4808                 :         uint64 data;
    4809               0 :         assert(fip->field_readcount==1);
    4810               0 :         assert(fip->field_passcount==0);
    4811               0 :         err=TIFFReadDirEntryLong8(tif,dp,&data);
    4812               0 :         if (err==TIFFReadDirEntryErrOk)
    4813                 :         {
    4814               0 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4815               0 :             return(0);
    4816                 :         }
    4817                 :       }
    4818               0 :       break;
    4819                 :     case TIFF_SETGET_FLOAT:
    4820                 :       {
    4821                 :         float data;
    4822               0 :         assert(fip->field_readcount==1);
    4823               0 :         assert(fip->field_passcount==0);
    4824               0 :         err=TIFFReadDirEntryFloat(tif,dp,&data);
    4825               0 :         if (err==TIFFReadDirEntryErrOk)
    4826                 :         {
    4827               0 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4828               0 :             return(0);
    4829                 :         }
    4830                 :       }
    4831               0 :       break;
    4832                 :     case TIFF_SETGET_DOUBLE:
    4833                 :       {
    4834                 :         double data;
    4835             130 :         assert(fip->field_readcount==1);
    4836             130 :         assert(fip->field_passcount==0);
    4837             130 :         err=TIFFReadDirEntryDouble(tif,dp,&data);
    4838             130 :         if (err==TIFFReadDirEntryErrOk)
    4839                 :         {
    4840             130 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4841               0 :             return(0);
    4842                 :         }
    4843                 :       }
    4844             130 :       break;
    4845                 :     case TIFF_SETGET_IFD8:
    4846                 :       {
    4847                 :         uint64 data;
    4848               9 :         assert(fip->field_readcount==1);
    4849               9 :         assert(fip->field_passcount==0);
    4850               9 :         err=TIFFReadDirEntryIfd8(tif,dp,&data);
    4851               9 :         if (err==TIFFReadDirEntryErrOk)
    4852                 :         {
    4853               9 :           if (!TIFFSetField(tif,dp->tdir_tag,data))
    4854               0 :             return(0);
    4855                 :         }
    4856                 :       }
    4857               9 :       break;
    4858                 :     case TIFF_SETGET_UINT16_PAIR:
    4859                 :       {
    4860                 :         uint16* data;
    4861               7 :         assert(fip->field_readcount==2);
    4862               7 :         assert(fip->field_passcount==0);
    4863               7 :         if (dp->tdir_count!=2) {
    4864               0 :           TIFFWarningExt(tif->tif_clientdata,module,
    4865                 :                    "incorrect count for field \"%s\", expected 2, got %d",
    4866                 :                    fip->field_name,(int)dp->tdir_count);
    4867               0 :           return(0);
    4868                 :         }
    4869               7 :         err=TIFFReadDirEntryShortArray(tif,dp,&data);
    4870               7 :         if (err==TIFFReadDirEntryErrOk)
    4871                 :         {
    4872                 :           int m;
    4873               7 :           m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
    4874               7 :           _TIFFfree(data);
    4875               7 :           if (!m)
    4876               0 :             return(0);
    4877                 :         }
    4878                 :       }
    4879               7 :       break;
    4880                 :     case TIFF_SETGET_C0_UINT8:
    4881                 :       {
    4882                 :         uint8* data;
    4883               0 :         assert(fip->field_readcount>=1);
    4884               0 :         assert(fip->field_passcount==0);
    4885               0 :         if (dp->tdir_count!=(uint64)fip->field_readcount) {
    4886               0 :           TIFFWarningExt(tif->tif_clientdata,module,
    4887                 :                    "incorrect count for field \"%s\", expected %d, got %d",
    4888                 :                    fip->field_name,(int) fip->field_readcount, (int)dp->tdir_count);
    4889               0 :           return 0;
    4890                 :         }
    4891                 :         else
    4892                 :         {
    4893               0 :           err=TIFFReadDirEntryByteArray(tif,dp,&data);
    4894               0 :           if (err==TIFFReadDirEntryErrOk)
    4895                 :           {
    4896                 :             int m;
    4897               0 :             m=TIFFSetField(tif,dp->tdir_tag,data);
    4898               0 :             if (data!=0)
    4899               0 :               _TIFFfree(data);
    4900               0 :             if (!m)
    4901               0 :               return(0);
    4902                 :           }
    4903                 :         }
    4904                 :       }
    4905               0 :       break;
    4906                 :     case TIFF_SETGET_C0_UINT16:
    4907                 :       {
    4908                 :         uint16* data;
    4909               0 :         assert(fip->field_readcount>=1);
    4910               0 :         assert(fip->field_passcount==0);
    4911               0 :         if (dp->tdir_count!=(uint64)fip->field_readcount)
    4912                 :                                     /* corrupt file */;
    4913                 :         else
    4914                 :         {
    4915               0 :           err=TIFFReadDirEntryShortArray(tif,dp,&data);
    4916               0 :           if (err==TIFFReadDirEntryErrOk)
    4917                 :           {
    4918                 :             int m;
    4919               0 :             m=TIFFSetField(tif,dp->tdir_tag,data);
    4920               0 :             if (data!=0)
    4921               0 :               _TIFFfree(data);
    4922               0 :             if (!m)
    4923               0 :               return(0);
    4924                 :           }
    4925                 :         }
    4926                 :       }
    4927               0 :       break;
    4928                 :     case TIFF_SETGET_C0_UINT32:
    4929                 :       {
    4930                 :         uint32* data;
    4931               0 :         assert(fip->field_readcount>=1);
    4932               0 :         assert(fip->field_passcount==0);
    4933               0 :         if (dp->tdir_count!=(uint64)fip->field_readcount)
    4934                 :                                     /* corrupt file */;
    4935                 :         else
    4936                 :         {
    4937               0 :           err=TIFFReadDirEntryLongArray(tif,dp,&data);
    4938               0 :           if (err==TIFFReadDirEntryErrOk)
    4939                 :           {
    4940                 :             int m;
    4941               0 :             m=TIFFSetField(tif,dp->tdir_tag,data);
    4942               0 :             if (data!=0)
    4943               0 :               _TIFFfree(data);
    4944               0 :             if (!m)
    4945               0 :               return(0);
    4946                 :           }
    4947                 :         }
    4948                 :       }
    4949               0 :       break;
    4950                 :     case TIFF_SETGET_C0_FLOAT:
    4951                 :       {
    4952                 :         float* data;
    4953             174 :         assert(fip->field_readcount>=1);
    4954             174 :         assert(fip->field_passcount==0);
    4955             174 :         if (dp->tdir_count!=(uint64)fip->field_readcount)
    4956                 :                                     /* corrupt file */;
    4957                 :         else
    4958                 :         {
    4959             174 :           err=TIFFReadDirEntryFloatArray(tif,dp,&data);
    4960             174 :           if (err==TIFFReadDirEntryErrOk)
    4961                 :           {
    4962                 :             int m;
    4963             174 :             m=TIFFSetField(tif,dp->tdir_tag,data);
    4964             174 :             if (data!=0)
    4965             174 :               _TIFFfree(data);
    4966             174 :             if (!m)
    4967               0 :               return(0);
    4968                 :           }
    4969                 :         }
    4970                 :       }
    4971             174 :       break;
    4972                 :     case TIFF_SETGET_C16_ASCII:
    4973                 :       {
    4974                 :         uint8* data;
    4975               0 :         assert(fip->field_readcount==TIFF_VARIABLE);
    4976               0 :         assert(fip->field_passcount==1);
    4977               0 :         if (dp->tdir_count>0xFFFF)
    4978               0 :           err=TIFFReadDirEntryErrCount;
    4979                 :         else
    4980                 :         {
    4981               0 :           err=TIFFReadDirEntryByteArray(tif,dp,&data);
    4982               0 :           if (err==TIFFReadDirEntryErrOk)
    4983                 :           {
    4984                 :             int m;
    4985               0 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    4986               0 :             if (data!=0)
    4987               0 :               _TIFFfree(data);
    4988               0 :             if (!m)
    4989               0 :               return(0);
    4990                 :           }
    4991                 :         }
    4992                 :       }
    4993               0 :       break;
    4994                 :     case TIFF_SETGET_C16_UINT8:
    4995                 :       {
    4996                 :         uint8* data;
    4997               0 :         assert(fip->field_readcount==TIFF_VARIABLE);
    4998               0 :         assert(fip->field_passcount==1);
    4999               0 :         if (dp->tdir_count>0xFFFF)
    5000               0 :           err=TIFFReadDirEntryErrCount;
    5001                 :         else
    5002                 :         {
    5003               0 :           err=TIFFReadDirEntryByteArray(tif,dp,&data);
    5004               0 :           if (err==TIFFReadDirEntryErrOk)
    5005                 :           {
    5006                 :             int m;
    5007               0 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5008               0 :             if (data!=0)
    5009               0 :               _TIFFfree(data);
    5010               0 :             if (!m)
    5011               0 :               return(0);
    5012                 :           }
    5013                 :         }
    5014                 :       }
    5015               0 :       break;
    5016                 :     case TIFF_SETGET_C16_UINT16:
    5017                 :       {
    5018                 :         uint16* data;
    5019            7026 :         assert(fip->field_readcount==TIFF_VARIABLE);
    5020            7026 :         assert(fip->field_passcount==1);
    5021            7026 :         if (dp->tdir_count>0xFFFF)
    5022               0 :           err=TIFFReadDirEntryErrCount;
    5023                 :         else
    5024                 :         {
    5025            7026 :           err=TIFFReadDirEntryShortArray(tif,dp,&data);
    5026            7026 :           if (err==TIFFReadDirEntryErrOk)
    5027                 :           {
    5028                 :             int m;
    5029            7026 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5030            7026 :             if (data!=0)
    5031            7026 :               _TIFFfree(data);
    5032            7026 :             if (!m)
    5033               0 :               return(0);
    5034                 :           }
    5035                 :         }
    5036                 :       }
    5037            7026 :       break;
    5038                 :     case TIFF_SETGET_C16_UINT32:
    5039                 :       {
    5040                 :         uint32* data;
    5041               0 :         assert(fip->field_readcount==TIFF_VARIABLE);
    5042               0 :         assert(fip->field_passcount==1);
    5043               0 :         if (dp->tdir_count>0xFFFF)
    5044               0 :           err=TIFFReadDirEntryErrCount;
    5045                 :         else
    5046                 :         {
    5047               0 :           err=TIFFReadDirEntryLongArray(tif,dp,&data);
    5048               0 :           if (err==TIFFReadDirEntryErrOk)
    5049                 :           {
    5050                 :             int m;
    5051               0 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5052               0 :             if (data!=0)
    5053               0 :               _TIFFfree(data);
    5054               0 :             if (!m)
    5055               0 :               return(0);
    5056                 :           }
    5057                 :         }
    5058                 :       }
    5059               0 :       break;
    5060                 :     case TIFF_SETGET_C16_UINT64:
    5061                 :       {
    5062                 :         uint64* data;
    5063               0 :         assert(fip->field_readcount==TIFF_VARIABLE);
    5064               0 :         assert(fip->field_passcount==1);
    5065               0 :         if (dp->tdir_count>0xFFFF)
    5066               0 :           err=TIFFReadDirEntryErrCount;
    5067                 :         else
    5068                 :         {
    5069               0 :           err=TIFFReadDirEntryLong8Array(tif,dp,&data);
    5070               0 :           if (err==TIFFReadDirEntryErrOk)
    5071                 :           {
    5072                 :             int m;
    5073               0 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5074               0 :             if (data!=0)
    5075               0 :               _TIFFfree(data);
    5076               0 :             if (!m)
    5077               0 :               return(0);
    5078                 :           }
    5079                 :         }
    5080                 :       }
    5081               0 :       break;
    5082                 :     case TIFF_SETGET_C16_FLOAT:
    5083                 :       {
    5084                 :         float* data;
    5085               0 :         assert(fip->field_readcount==TIFF_VARIABLE);
    5086               0 :         assert(fip->field_passcount==1);
    5087               0 :         if (dp->tdir_count>0xFFFF)
    5088               0 :           err=TIFFReadDirEntryErrCount;
    5089                 :         else
    5090                 :         {
    5091               0 :           err=TIFFReadDirEntryFloatArray(tif,dp,&data);
    5092               0 :           if (err==TIFFReadDirEntryErrOk)
    5093                 :           {
    5094                 :             int m;
    5095               0 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5096               0 :             if (data!=0)
    5097               0 :               _TIFFfree(data);
    5098               0 :             if (!m)
    5099               0 :               return(0);
    5100                 :           }
    5101                 :         }
    5102                 :       }
    5103               0 :       break;
    5104                 :     case TIFF_SETGET_C16_DOUBLE:
    5105                 :       {
    5106                 :         double* data;
    5107           15966 :         assert(fip->field_readcount==TIFF_VARIABLE);
    5108           15966 :         assert(fip->field_passcount==1);
    5109           15966 :         if (dp->tdir_count>0xFFFF)
    5110               0 :           err=TIFFReadDirEntryErrCount;
    5111                 :         else
    5112                 :         {
    5113           15966 :           err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
    5114           15966 :           if (err==TIFFReadDirEntryErrOk)
    5115                 :           {
    5116                 :             int m;
    5117           15966 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5118           15966 :             if (data!=0)
    5119           15966 :               _TIFFfree(data);
    5120           15966 :             if (!m)
    5121               0 :               return(0);
    5122                 :           }
    5123                 :         }
    5124                 :       }
    5125           15966 :       break;
    5126                 :     case TIFF_SETGET_C16_IFD8:
    5127                 :       {
    5128                 :         uint64* data;
    5129               0 :         assert(fip->field_readcount==TIFF_VARIABLE);
    5130               0 :         assert(fip->field_passcount==1);
    5131               0 :         if (dp->tdir_count>0xFFFF)
    5132               0 :           err=TIFFReadDirEntryErrCount;
    5133                 :         else
    5134                 :         {
    5135               0 :           err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
    5136               0 :           if (err==TIFFReadDirEntryErrOk)
    5137                 :           {
    5138                 :             int m;
    5139               0 :             m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
    5140               0 :             if (data!=0)
    5141               0 :               _TIFFfree(data);
    5142               0 :             if (!m)
    5143               0 :               return(0);
    5144                 :           }
    5145                 :         }
    5146                 :       }
    5147               0 :       break;
    5148                 :     case TIFF_SETGET_C32_ASCII:
    5149                 :       {
    5150                 :         uint8* data;
    5151               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5152               0 :         assert(fip->field_passcount==1);
    5153               0 :         err=TIFFReadDirEntryByteArray(tif,dp,&data);
    5154               0 :         if (err==TIFFReadDirEntryErrOk)
    5155                 :         {
    5156                 :           int m;
    5157               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5158               0 :           if (data!=0)
    5159               0 :             _TIFFfree(data);
    5160               0 :           if (!m)
    5161               0 :             return(0);
    5162                 :         }
    5163                 :       }
    5164               0 :       break;
    5165                 :     case TIFF_SETGET_C32_UINT8:
    5166                 :       {
    5167                 :         uint8* data;
    5168             248 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5169             248 :         assert(fip->field_passcount==1);
    5170             248 :         err=TIFFReadDirEntryByteArray(tif,dp,&data);
    5171             248 :         if (err==TIFFReadDirEntryErrOk)
    5172                 :         {
    5173                 :           int m;
    5174             248 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5175             248 :           if (data!=0)
    5176             248 :             _TIFFfree(data);
    5177             248 :           if (!m)
    5178               0 :             return(0);
    5179                 :         }
    5180                 :       }
    5181             248 :       break;
    5182                 :     case TIFF_SETGET_C32_SINT8:
    5183                 :       {
    5184               0 :         int8* data = NULL;
    5185               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5186               0 :         assert(fip->field_passcount==1);
    5187               0 :         err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
    5188               0 :         if (err==TIFFReadDirEntryErrOk)
    5189                 :         {
    5190                 :           int m;
    5191               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5192               0 :           if (data!=0)
    5193               0 :             _TIFFfree(data);
    5194               0 :           if (!m)
    5195               0 :             return(0);
    5196                 :         }
    5197                 :       }
    5198               0 :       break;
    5199                 :     case TIFF_SETGET_C32_UINT16:
    5200                 :       {
    5201                 :         uint16* data;
    5202               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5203               0 :         assert(fip->field_passcount==1);
    5204               0 :         err=TIFFReadDirEntryShortArray(tif,dp,&data);
    5205               0 :         if (err==TIFFReadDirEntryErrOk)
    5206                 :         {
    5207                 :           int m;
    5208               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5209               0 :           if (data!=0)
    5210               0 :             _TIFFfree(data);
    5211               0 :           if (!m)
    5212               0 :             return(0);
    5213                 :         }
    5214                 :       }
    5215               0 :       break;
    5216                 :     case TIFF_SETGET_C32_SINT16:
    5217                 :       {
    5218               0 :         int16* data = NULL;
    5219               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5220               0 :         assert(fip->field_passcount==1);
    5221               0 :         err=TIFFReadDirEntrySshortArray(tif,dp,&data);
    5222               0 :         if (err==TIFFReadDirEntryErrOk)
    5223                 :         {
    5224                 :           int m;
    5225               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5226               0 :           if (data!=0)
    5227               0 :             _TIFFfree(data);
    5228               0 :           if (!m)
    5229               0 :             return(0);
    5230                 :         }
    5231                 :       }
    5232               0 :       break;
    5233                 :     case TIFF_SETGET_C32_UINT32:
    5234                 :       {
    5235                 :         uint32* data;
    5236               1 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5237               1 :         assert(fip->field_passcount==1);
    5238               1 :         err=TIFFReadDirEntryLongArray(tif,dp,&data);
    5239               1 :         if (err==TIFFReadDirEntryErrOk)
    5240                 :         {
    5241                 :           int m;
    5242               1 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5243               1 :           if (data!=0)
    5244               1 :             _TIFFfree(data);
    5245               1 :           if (!m)
    5246               0 :             return(0);
    5247                 :         }
    5248                 :       }
    5249               1 :       break;
    5250                 :     case TIFF_SETGET_C32_SINT32:
    5251                 :       {
    5252               0 :         int32* data = NULL;
    5253               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5254               0 :         assert(fip->field_passcount==1);
    5255               0 :         err=TIFFReadDirEntrySlongArray(tif,dp,&data);
    5256               0 :         if (err==TIFFReadDirEntryErrOk)
    5257                 :         {
    5258                 :           int m;
    5259               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5260               0 :           if (data!=0)
    5261               0 :             _TIFFfree(data);
    5262               0 :           if (!m)
    5263               0 :             return(0);
    5264                 :         }
    5265                 :       }
    5266               0 :       break;
    5267                 :     case TIFF_SETGET_C32_UINT64:
    5268                 :       {
    5269                 :         uint64* data;
    5270               3 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5271               3 :         assert(fip->field_passcount==1);
    5272               3 :         err=TIFFReadDirEntryLong8Array(tif,dp,&data);
    5273               3 :         if (err==TIFFReadDirEntryErrOk)
    5274                 :         {
    5275                 :           int m;
    5276               3 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5277               3 :           if (data!=0)
    5278               3 :             _TIFFfree(data);
    5279               3 :           if (!m)
    5280               0 :             return(0);
    5281                 :         }
    5282                 :       }
    5283               3 :       break;
    5284                 :     case TIFF_SETGET_C32_SINT64:
    5285                 :       {
    5286               0 :         int64* data = NULL;
    5287               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5288               0 :         assert(fip->field_passcount==1);
    5289               0 :         err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
    5290               0 :         if (err==TIFFReadDirEntryErrOk)
    5291                 :         {
    5292                 :           int m;
    5293               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5294               0 :           if (data!=0)
    5295               0 :             _TIFFfree(data);
    5296               0 :           if (!m)
    5297               0 :             return(0);
    5298                 :         }
    5299                 :       }
    5300               0 :       break;
    5301                 :     case TIFF_SETGET_C32_FLOAT:
    5302                 :       {
    5303                 :         float* data;
    5304               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5305               0 :         assert(fip->field_passcount==1);
    5306               0 :         err=TIFFReadDirEntryFloatArray(tif,dp,&data);
    5307               0 :         if (err==TIFFReadDirEntryErrOk)
    5308                 :         {
    5309                 :           int m;
    5310               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5311               0 :           if (data!=0)
    5312               0 :             _TIFFfree(data);
    5313               0 :           if (!m)
    5314               0 :             return(0);
    5315                 :         }
    5316                 :       }
    5317               0 :       break;
    5318                 :     case TIFF_SETGET_C32_DOUBLE:
    5319                 :       {
    5320                 :         double* data;
    5321               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5322               0 :         assert(fip->field_passcount==1);
    5323               0 :         err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
    5324               0 :         if (err==TIFFReadDirEntryErrOk)
    5325                 :         {
    5326                 :           int m;
    5327               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5328               0 :           if (data!=0)
    5329               0 :             _TIFFfree(data);
    5330               0 :           if (!m)
    5331               0 :             return(0);
    5332                 :         }
    5333                 :       }
    5334               0 :       break;
    5335                 :     case TIFF_SETGET_C32_IFD8:
    5336                 :       {
    5337                 :         uint64* data;
    5338               0 :         assert(fip->field_readcount==TIFF_VARIABLE2);
    5339               0 :         assert(fip->field_passcount==1);
    5340               0 :         err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
    5341               0 :         if (err==TIFFReadDirEntryErrOk)
    5342                 :         {
    5343                 :           int m;
    5344               0 :           m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
    5345               0 :           if (data!=0)
    5346               0 :             _TIFFfree(data);
    5347               0 :           if (!m)
    5348               0 :             return(0);
    5349                 :         }
    5350                 :       }
    5351               0 :       break;
    5352                 :     default:
    5353               0 :       assert(0);    /* we should never get here */
    5354                 :       break;
    5355                 :   }
    5356           96943 :   if (err!=TIFFReadDirEntryErrOk)
    5357                 :   {
    5358               2 :     TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",recover);
    5359               2 :     return(0);
    5360                 :   }
    5361           96941 :   return(1);
    5362                 : }
    5363                 : 
    5364                 : /*
    5365                 :  * Fetch a set of offsets or lengths.
    5366                 :  * While this routine says "strips", in fact it's also used for tiles.
    5367                 :  */
    5368                 : static int
    5369           19142 : TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
    5370                 : {
    5371                 :   static const char module[] = "TIFFFetchStripThing";
    5372                 :   enum TIFFReadDirEntryErr err;
    5373                 :   uint64* data;
    5374           19142 :   err=TIFFReadDirEntryLong8Array(tif,dir,&data);
    5375           19142 :   if (err!=TIFFReadDirEntryErrOk)
    5376                 :   {
    5377               0 :     const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag); 
    5378               0 :     TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
    5379               0 :     return(0);
    5380                 :   }
    5381           19142 :   if (dir->tdir_count!=(uint64)nstrips)
    5382                 :   {
    5383                 :     uint64* resizeddata;
    5384               0 :     resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
    5385               0 :     if (resizeddata==0) {
    5386               0 :       _TIFFfree(data);
    5387               0 :       return(0);
    5388                 :     }
    5389               0 :     if (dir->tdir_count<(uint64)nstrips)
    5390                 :     {
    5391               0 :       _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
    5392               0 :       _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
    5393                 :     }
    5394                 :     else
    5395               0 :       _TIFFmemcpy(resizeddata,data,nstrips*sizeof(uint64));
    5396               0 :     _TIFFfree(data);
    5397               0 :     data=resizeddata;
    5398                 :   }
    5399           19142 :   *lpp=data;
    5400           19142 :   return(1);
    5401                 : }
    5402                 : 
    5403                 : /*
    5404                 :  * Fetch and set the SubjectDistance EXIF tag.
    5405                 :  */
    5406                 : static int
    5407               0 : TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
    5408                 : {
    5409                 :   static const char module[] = "TIFFFetchSubjectDistance";
    5410                 :   enum TIFFReadDirEntryErr err;
    5411                 :   UInt64Aligned_t m;
    5412               0 :     m.l=0;
    5413                 :   assert(sizeof(double)==8);
    5414                 :   assert(sizeof(uint64)==8);
    5415                 :   assert(sizeof(uint32)==4);
    5416               0 :   if (dir->tdir_count!=1)
    5417               0 :     err=TIFFReadDirEntryErrCount;
    5418               0 :   else if (dir->tdir_type!=TIFF_RATIONAL)
    5419               0 :     err=TIFFReadDirEntryErrType;
    5420                 :   else
    5421                 :   {
    5422               0 :     if (!(tif->tif_flags&TIFF_BIGTIFF))
    5423                 :     {
    5424                 :       uint32 offset;
    5425               0 :       offset=*(uint32*)(&dir->tdir_offset);
    5426               0 :       if (tif->tif_flags&TIFF_SWAB)
    5427               0 :         TIFFSwabLong(&offset);
    5428               0 :       err=TIFFReadDirEntryData(tif,offset,8,m.i);
    5429                 :     }
    5430                 :     else
    5431                 :     {
    5432               0 :       m.l=dir->tdir_offset.toff_long8;
    5433               0 :       err=TIFFReadDirEntryErrOk;
    5434                 :     }
    5435                 :   }
    5436               0 :   if (err==TIFFReadDirEntryErrOk)
    5437                 :   {
    5438                 :     double n;
    5439               0 :     if (tif->tif_flags&TIFF_SWAB)
    5440               0 :       TIFFSwabArrayOfLong(m.i,2);
    5441               0 :     if (m.i[0]==0)
    5442               0 :       n=0.0;
    5443               0 :     else if (m.i[0]==0xFFFFFFFF)
    5444                 :       /*
    5445                 :        * XXX: Numerator 0xFFFFFFFF means that we have infinite
    5446                 :        * distance. Indicate that with a negative floating point
    5447                 :        * SubjectDistance value.
    5448                 :        */
    5449               0 :       n=-1.0;
    5450                 :     else
    5451               0 :       n=(double)m.i[0]/(double)m.i[1];
    5452               0 :     return(TIFFSetField(tif,dir->tdir_tag,n));
    5453                 :   }
    5454                 :   else
    5455                 :   {
    5456               0 :     TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
    5457               0 :     return(0);
    5458                 :   }
    5459                 : }
    5460                 : 
    5461                 : /*
    5462                 :  * Replace a single strip (tile) of uncompressed data by multiple strips
    5463                 :  * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
    5464                 :  * dealing with large images or for dealing with machines with a limited
    5465                 :  * amount memory.
    5466                 :  */
    5467                 : static void
    5468            3417 : ChopUpSingleUncompressedStrip(TIFF* tif)
    5469                 : {
    5470            3417 :   register TIFFDirectory *td = &tif->tif_dir;
    5471                 :   uint64 bytecount;
    5472                 :   uint64 offset;
    5473                 :   uint32 rowblock;
    5474                 :   uint64 rowblockbytes;
    5475                 :   uint64 stripbytes;
    5476                 :   uint32 strip;
    5477                 :   uint64 nstrips64;
    5478                 :   uint32 nstrips32;
    5479                 :   uint32 rowsperstrip;
    5480                 :   uint64* newcounts;
    5481                 :   uint64* newoffsets;
    5482                 : 
    5483            3417 :   bytecount = td->td_stripbytecount[0];
    5484            3417 :   offset = td->td_stripoffset[0];
    5485            3417 :   assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
    5486            3417 :   if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
    5487               0 :       (!isUpSampled(tif)))
    5488               0 :     rowblock = td->td_ycbcrsubsampling[1];
    5489                 :   else
    5490            3417 :     rowblock = 1;
    5491            3417 :   rowblockbytes = TIFFVTileSize64(tif, rowblock);
    5492                 :   /*
    5493                 :    * Make the rows hold at least one scanline, but fill specified amount
    5494                 :    * of data if possible.
    5495                 :    */
    5496            3417 :   if (rowblockbytes > STRIP_SIZE_DEFAULT) {
    5497               2 :     stripbytes = rowblockbytes;
    5498               2 :     rowsperstrip = rowblock;
    5499            3415 :   } else if (rowblockbytes > 0 ) {
    5500                 :     uint32 rowblocksperstrip;
    5501            3415 :     rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
    5502            3415 :     rowsperstrip = rowblocksperstrip * rowblock;
    5503            3415 :     stripbytes = rowblocksperstrip * rowblockbytes;
    5504                 :   }
    5505                 :   else
    5506               0 :       return;
    5507                 : 
    5508                 :   /*
    5509                 :    * never increase the number of strips in an image
    5510                 :    */
    5511            3417 :   if (rowsperstrip >= td->td_rowsperstrip)
    5512            3388 :     return;
    5513              29 :   nstrips64 = TIFFhowmany_64(bytecount, stripbytes);
    5514              29 :   if ((nstrips64==0)||(nstrips64>0xFFFFFFFF)) /* something is wonky, do nothing. */
    5515               0 :       return;
    5516              29 :   nstrips32 = (uint32)nstrips64;
    5517                 : 
    5518              29 :   newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
    5519                 :         "for chopped \"StripByteCounts\" array");
    5520              29 :   newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
    5521                 :         "for chopped \"StripOffsets\" array");
    5522              29 :   if (newcounts == NULL || newoffsets == NULL) {
    5523                 :     /*
    5524                 :      * Unable to allocate new strip information, give up and use
    5525                 :      * the original one strip information.
    5526                 :      */
    5527               0 :     if (newcounts != NULL)
    5528               0 :       _TIFFfree(newcounts);
    5529               0 :     if (newoffsets != NULL)
    5530               0 :       _TIFFfree(newoffsets);
    5531               0 :     return;
    5532                 :   }
    5533                 :   /*
    5534                 :    * Fill the strip information arrays with new bytecounts and offsets
    5535                 :    * that reflect the broken-up format.
    5536                 :    */
    5537             806 :   for (strip = 0; strip < nstrips32; strip++) {
    5538             777 :     if (stripbytes > bytecount)
    5539               5 :       stripbytes = bytecount;
    5540             777 :     newcounts[strip] = stripbytes;
    5541             777 :     newoffsets[strip] = offset;
    5542             777 :     offset += stripbytes;
    5543             777 :     bytecount -= stripbytes;
    5544                 :   }
    5545                 :   /*
    5546                 :    * Replace old single strip info with multi-strip info.
    5547                 :    */
    5548              29 :   td->td_stripsperimage = td->td_nstrips = nstrips32;
    5549              29 :   TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
    5550                 : 
    5551              29 :   _TIFFfree(td->td_stripbytecount);
    5552              29 :   _TIFFfree(td->td_stripoffset);
    5553              29 :   td->td_stripbytecount = newcounts;
    5554              29 :   td->td_stripoffset = newoffsets;
    5555              29 :   td->td_stripbytecountsorted = 1;
    5556                 : }
    5557                 : 
    5558          314515 : int _TIFFFillStriles( TIFF *tif )
    5559                 : {
    5560                 : #if defined(DEFER_STRILE_LOAD)
    5561          314515 :         register TIFFDirectory *td = &tif->tif_dir;
    5562          314515 :         int return_value = 1;
    5563                 : 
    5564          314515 :         if( td->td_stripoffset != NULL )
    5565          303446 :                 return 1;
    5566                 : 
    5567           11069 :         if( td->td_stripoffset_entry.tdir_count == 0 )
    5568            1498 :                 return 0;
    5569                 : 
    5570            9571 :         if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
    5571                 :                                  td->td_nstrips,&td->td_stripoffset))
    5572                 :         {
    5573               0 :                 return_value = 0;
    5574                 :         }
    5575                 : 
    5576            9571 :         if (!TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
    5577                 :                                  td->td_nstrips,&td->td_stripbytecount))
    5578                 :         {
    5579               0 :                 return_value = 0;
    5580                 :         }
    5581                 : 
    5582            9571 :         _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
    5583            9571 :         _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
    5584                 : 
    5585            9571 :   if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
    5586                 :     uint32 strip;
    5587                 : 
    5588            1099 :     tif->tif_dir.td_stripbytecountsorted = 1;
    5589          330872 :     for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
    5590          659770 :       if (tif->tif_dir.td_stripoffset[strip - 1] >
    5591          329885 :           tif->tif_dir.td_stripoffset[strip]) {
    5592             112 :         tif->tif_dir.td_stripbytecountsorted = 0;
    5593             112 :         break;
    5594                 :       }
    5595                 :     }
    5596                 :   }
    5597                 : 
    5598            9571 :         return return_value;
    5599                 : #else /* !defined(DEFER_STRILE_LOAD) */
    5600                 :         (void) tif;
    5601                 :         return 1;
    5602                 : #endif 
    5603                 : }
    5604                 : 
    5605                 : 
    5606                 : /* vim: set ts=8 sts=8 sw=8 noet: */
    5607                 : /*
    5608                 :  * Local Variables:
    5609                 :  * mode: c
    5610                 :  * c-basic-offset: 8
    5611                 :  * fill-column: 78
    5612                 :  * End:
    5613                 :  */

Generated by: LCOV version 1.7