LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_dirread.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 1465 4212 34.8 %
Date: 2026-03-09 02:11:35 Functions: 58 120 48.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1988-1997 Sam Leffler
       3             :  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
       4             :  *
       5             :  * Permission to use, copy, modify, distribute, and sell this software and
       6             :  * its documentation for any purpose is hereby granted without fee, provided
       7             :  * that (i) the above copyright notices and this permission notice appear in
       8             :  * all copies of the software and related documentation, and (ii) the names of
       9             :  * Sam Leffler and Silicon Graphics may not be used in any advertising or
      10             :  * publicity relating to the software without the specific, prior written
      11             :  * permission of Sam Leffler and Silicon Graphics.
      12             :  *
      13             :  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
      14             :  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
      15             :  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
      16             :  *
      17             :  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
      18             :  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
      19             :  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
      20             :  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
      21             :  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
      22             :  * OF THIS SOFTWARE.
      23             :  */
      24             : 
      25             : /*
      26             :  * TIFF Library.
      27             :  *
      28             :  * Directory Read Support Routines.
      29             :  */
      30             : 
      31             : /* Suggested pending improvements:
      32             :  * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
      33             :  *   the pointer to the appropriate TIFFField structure early on in
      34             :  *   TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
      35             :  */
      36             : 
      37             : #include "tiffconf.h"
      38             : #include "tiffiop.h"
      39             : #include <float.h>
      40             : #include <limits.h>
      41             : #include <stdlib.h>
      42             : #include <string.h>
      43             : 
      44             : #define FAILED_FII ((uint32_t)-1)
      45             : 
      46             : #ifdef HAVE_IEEEFP
      47             : #define TIFFCvtIEEEFloatToNative(tif, n, fp)
      48             : #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
      49             : #else
      50             : /* If your machine does not support IEEE floating point then you will need to
      51             :  * add support to tif_machdep.c to convert between the native format and
      52             :  * IEEE format. */
      53             : extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *);
      54             : extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *);
      55             : #endif
      56             : 
      57             : enum TIFFReadDirEntryErr
      58             : {
      59             :     TIFFReadDirEntryErrOk = 0,
      60             :     TIFFReadDirEntryErrCount = 1,
      61             :     TIFFReadDirEntryErrType = 2,
      62             :     TIFFReadDirEntryErrIo = 3,
      63             :     TIFFReadDirEntryErrRange = 4,
      64             :     TIFFReadDirEntryErrPsdif = 5,
      65             :     TIFFReadDirEntryErrSizesan = 6,
      66             :     TIFFReadDirEntryErrAlloc = 7,
      67             : };
      68             : 
      69             : static enum TIFFReadDirEntryErr
      70             : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value);
      71             : static enum TIFFReadDirEntryErr
      72             : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value);
      73             : static enum TIFFReadDirEntryErr
      74             : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value);
      75             : static enum TIFFReadDirEntryErr
      76             : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value);
      77             : static enum TIFFReadDirEntryErr
      78             : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value);
      79             : static enum TIFFReadDirEntryErr
      80             : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value);
      81             : static enum TIFFReadDirEntryErr
      82             : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
      83             : static enum TIFFReadDirEntryErr
      84             : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value);
      85             : static enum TIFFReadDirEntryErr
      86             : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value);
      87             : static enum TIFFReadDirEntryErr
      88             : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
      89             : static enum TIFFReadDirEntryErr
      90             : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
      91             : 
      92             : static enum TIFFReadDirEntryErr
      93             : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
      94             :                       uint32_t desttypesize, void **value);
      95             : static enum TIFFReadDirEntryErr
      96             : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value);
      97             : static enum TIFFReadDirEntryErr
      98             : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value);
      99             : static enum TIFFReadDirEntryErr
     100             : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value);
     101             : static enum TIFFReadDirEntryErr
     102             : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value);
     103             : static enum TIFFReadDirEntryErr
     104             : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value);
     105             : static enum TIFFReadDirEntryErr
     106             : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value);
     107             : static enum TIFFReadDirEntryErr
     108             : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
     109             : static enum TIFFReadDirEntryErr
     110             : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value);
     111             : static enum TIFFReadDirEntryErr
     112             : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value);
     113             : static enum TIFFReadDirEntryErr
     114             : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value);
     115             : static enum TIFFReadDirEntryErr
     116             : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
     117             : 
     118             : static enum TIFFReadDirEntryErr
     119             : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
     120             :                                uint16_t *value);
     121             : 
     122             : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
     123             :                                         uint8_t *value);
     124             : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
     125             :                                          int8_t *value);
     126             : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
     127             :                                          uint16_t *value);
     128             : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
     129             :                                           int16_t *value);
     130             : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
     131             :                                         uint32_t *value);
     132             : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
     133             :                                          int32_t *value);
     134             : static enum TIFFReadDirEntryErr
     135             : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry,
     136             :                              uint64_t *value);
     137             : static enum TIFFReadDirEntryErr
     138             : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry,
     139             :                               int64_t *value);
     140             : static enum TIFFReadDirEntryErr
     141             : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
     142             :                                 double *value);
     143             : static enum TIFFReadDirEntryErr
     144             : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
     145             :                                  double *value);
     146             : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
     147             :                                          float *value);
     148             : static enum TIFFReadDirEntryErr
     149             : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
     150             : #if 0
     151             : static enum TIFFReadDirEntryErr
     152             : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
     153             :                                       TIFFRational_t *value);
     154             : #endif
     155             : static enum TIFFReadDirEntryErr
     156             : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
     157             : static enum TIFFReadDirEntryErr
     158             : TIFFReadDirEntryCheckRangeByteShort(uint16_t value);
     159             : static enum TIFFReadDirEntryErr
     160             : TIFFReadDirEntryCheckRangeByteSshort(int16_t value);
     161             : static enum TIFFReadDirEntryErr
     162             : TIFFReadDirEntryCheckRangeByteLong(uint32_t value);
     163             : static enum TIFFReadDirEntryErr
     164             : TIFFReadDirEntryCheckRangeByteSlong(int32_t value);
     165             : static enum TIFFReadDirEntryErr
     166             : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value);
     167             : static enum TIFFReadDirEntryErr
     168             : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value);
     169             : 
     170             : static enum TIFFReadDirEntryErr
     171             : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value);
     172             : static enum TIFFReadDirEntryErr
     173             : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value);
     174             : static enum TIFFReadDirEntryErr
     175             : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value);
     176             : static enum TIFFReadDirEntryErr
     177             : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value);
     178             : static enum TIFFReadDirEntryErr
     179             : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value);
     180             : static enum TIFFReadDirEntryErr
     181             : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value);
     182             : static enum TIFFReadDirEntryErr
     183             : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value);
     184             : 
     185             : static enum TIFFReadDirEntryErr
     186             : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value);
     187             : static enum TIFFReadDirEntryErr
     188             : TIFFReadDirEntryCheckRangeShortSshort(int16_t value);
     189             : static enum TIFFReadDirEntryErr
     190             : TIFFReadDirEntryCheckRangeShortLong(uint32_t value);
     191             : static enum TIFFReadDirEntryErr
     192             : TIFFReadDirEntryCheckRangeShortSlong(int32_t value);
     193             : static enum TIFFReadDirEntryErr
     194             : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value);
     195             : static enum TIFFReadDirEntryErr
     196             : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value);
     197             : 
     198             : static enum TIFFReadDirEntryErr
     199             : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value);
     200             : static enum TIFFReadDirEntryErr
     201             : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value);
     202             : static enum TIFFReadDirEntryErr
     203             : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value);
     204             : static enum TIFFReadDirEntryErr
     205             : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value);
     206             : static enum TIFFReadDirEntryErr
     207             : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value);
     208             : 
     209             : static enum TIFFReadDirEntryErr
     210             : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value);
     211             : static enum TIFFReadDirEntryErr
     212             : TIFFReadDirEntryCheckRangeLongSshort(int16_t value);
     213             : static enum TIFFReadDirEntryErr
     214             : TIFFReadDirEntryCheckRangeLongSlong(int32_t value);
     215             : static enum TIFFReadDirEntryErr
     216             : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value);
     217             : static enum TIFFReadDirEntryErr
     218             : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value);
     219             : 
     220             : static enum TIFFReadDirEntryErr
     221             : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value);
     222             : static enum TIFFReadDirEntryErr
     223             : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value);
     224             : static enum TIFFReadDirEntryErr
     225             : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value);
     226             : 
     227             : static enum TIFFReadDirEntryErr
     228             : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value);
     229             : static enum TIFFReadDirEntryErr
     230             : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value);
     231             : static enum TIFFReadDirEntryErr
     232             : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value);
     233             : static enum TIFFReadDirEntryErr
     234             : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value);
     235             : 
     236             : static enum TIFFReadDirEntryErr
     237             : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value);
     238             : 
     239             : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
     240             :                                                      tmsize_t size, void *dest);
     241             : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
     242             :                                       const char *module, const char *tagname,
     243             :                                       int recover);
     244             : 
     245             : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
     246             :                                         uint16_t dircount);
     247             : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
     248             :                                                 uint16_t dircount,
     249             :                                                 uint16_t tagid);
     250             : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
     251             :                                            uint32_t *fii);
     252             : 
     253             : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
     254             :                                    uint16_t dircount);
     255             : static void MissingRequired(TIFF *, const char *);
     256             : static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
     257             : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
     258             :                                    TIFFDirEntry **pdir, uint64_t *nextdiroff);
     259             : static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover);
     260             : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
     261             :                                uint64_t **lpp);
     262             : static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *);
     263             : static void ChopUpSingleUncompressedStrip(TIFF *);
     264             : static void TryChopUpUncompressedBigTiff(TIFF *);
     265             : static uint64_t TIFFReadUInt64(const uint8_t *value);
     266             : static int _TIFFGetMaxColorChannels(uint16_t photometric);
     267             : 
     268             : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount);
     269             : 
     270             : typedef union _UInt64Aligned_t
     271             : {
     272             :     double d;
     273             :     uint64_t l;
     274             :     uint32_t i[2];
     275             :     uint16_t s[4];
     276             :     uint8_t c[8];
     277             : } UInt64Aligned_t;
     278             : 
     279             : /*
     280             :   Unaligned safe copy of a uint64_t value from an octet array.
     281             : */
     282       24494 : static uint64_t TIFFReadUInt64(const uint8_t *value)
     283             : {
     284             :     UInt64Aligned_t result;
     285             : 
     286       24494 :     result.c[0] = value[0];
     287       24494 :     result.c[1] = value[1];
     288       24494 :     result.c[2] = value[2];
     289       24494 :     result.c[3] = value[3];
     290       24494 :     result.c[4] = value[4];
     291       24494 :     result.c[5] = value[5];
     292       24494 :     result.c[6] = value[6];
     293       24494 :     result.c[7] = value[7];
     294             : 
     295       24494 :     return result.l;
     296             : }
     297             : 
     298             : static enum TIFFReadDirEntryErr
     299           0 : TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
     300             : {
     301             :     enum TIFFReadDirEntryErr err;
     302           0 :     if (direntry->tdir_count != 1)
     303           0 :         return (TIFFReadDirEntryErrCount);
     304           0 :     switch (direntry->tdir_type)
     305             :     {
     306           0 :         case TIFF_BYTE:
     307             :         case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
     308             :                                 field_readcount==1 */
     309           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, value);
     310           0 :             return (TIFFReadDirEntryErrOk);
     311           0 :         case TIFF_SBYTE:
     312             :         {
     313             :             int8_t m;
     314           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     315           0 :             err = TIFFReadDirEntryCheckRangeByteSbyte(m);
     316           0 :             if (err != TIFFReadDirEntryErrOk)
     317           0 :                 return (err);
     318           0 :             *value = (uint8_t)m;
     319           0 :             return (TIFFReadDirEntryErrOk);
     320             :         }
     321           0 :         case TIFF_SHORT:
     322             :         {
     323             :             uint16_t m;
     324           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     325           0 :             err = TIFFReadDirEntryCheckRangeByteShort(m);
     326           0 :             if (err != TIFFReadDirEntryErrOk)
     327           0 :                 return (err);
     328           0 :             *value = (uint8_t)m;
     329           0 :             return (TIFFReadDirEntryErrOk);
     330             :         }
     331           0 :         case TIFF_SSHORT:
     332             :         {
     333             :             int16_t m;
     334           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     335           0 :             err = TIFFReadDirEntryCheckRangeByteSshort(m);
     336           0 :             if (err != TIFFReadDirEntryErrOk)
     337           0 :                 return (err);
     338           0 :             *value = (uint8_t)m;
     339           0 :             return (TIFFReadDirEntryErrOk);
     340             :         }
     341           0 :         case TIFF_LONG:
     342             :         {
     343             :             uint32_t m;
     344           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     345           0 :             err = TIFFReadDirEntryCheckRangeByteLong(m);
     346           0 :             if (err != TIFFReadDirEntryErrOk)
     347           0 :                 return (err);
     348           0 :             *value = (uint8_t)m;
     349           0 :             return (TIFFReadDirEntryErrOk);
     350             :         }
     351           0 :         case TIFF_SLONG:
     352             :         {
     353             :             int32_t m;
     354           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     355           0 :             err = TIFFReadDirEntryCheckRangeByteSlong(m);
     356           0 :             if (err != TIFFReadDirEntryErrOk)
     357           0 :                 return (err);
     358           0 :             *value = (uint8_t)m;
     359           0 :             return (TIFFReadDirEntryErrOk);
     360             :         }
     361           0 :         case TIFF_LONG8:
     362             :         {
     363             :             uint64_t m;
     364           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     365           0 :             if (err != TIFFReadDirEntryErrOk)
     366           0 :                 return (err);
     367           0 :             err = TIFFReadDirEntryCheckRangeByteLong8(m);
     368           0 :             if (err != TIFFReadDirEntryErrOk)
     369           0 :                 return (err);
     370           0 :             *value = (uint8_t)m;
     371           0 :             return (TIFFReadDirEntryErrOk);
     372             :         }
     373           0 :         case TIFF_SLONG8:
     374             :         {
     375             :             int64_t m;
     376           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     377           0 :             if (err != TIFFReadDirEntryErrOk)
     378           0 :                 return (err);
     379           0 :             err = TIFFReadDirEntryCheckRangeByteSlong8(m);
     380           0 :             if (err != TIFFReadDirEntryErrOk)
     381           0 :                 return (err);
     382           0 :             *value = (uint8_t)m;
     383           0 :             return (TIFFReadDirEntryErrOk);
     384             :         }
     385           0 :         default:
     386           0 :             return (TIFFReadDirEntryErrType);
     387             :     }
     388             : }
     389             : 
     390             : static enum TIFFReadDirEntryErr
     391           0 : TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
     392             : {
     393             :     enum TIFFReadDirEntryErr err;
     394           0 :     if (direntry->tdir_count != 1)
     395           0 :         return (TIFFReadDirEntryErrCount);
     396           0 :     switch (direntry->tdir_type)
     397             :     {
     398           0 :         case TIFF_BYTE:
     399             :         case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
     400             :                                 field_readcount==1 */
     401             :         {
     402             :             uint8_t m;
     403           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     404           0 :             err = TIFFReadDirEntryCheckRangeSbyteByte(m);
     405           0 :             if (err != TIFFReadDirEntryErrOk)
     406           0 :                 return (err);
     407           0 :             *value = (int8_t)m;
     408           0 :             return (TIFFReadDirEntryErrOk);
     409             :         }
     410           0 :         case TIFF_SBYTE:
     411             :         {
     412           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
     413           0 :             return (TIFFReadDirEntryErrOk);
     414             :         }
     415           0 :         case TIFF_SHORT:
     416             :         {
     417             :             uint16_t m;
     418           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     419           0 :             err = TIFFReadDirEntryCheckRangeSbyteShort(m);
     420           0 :             if (err != TIFFReadDirEntryErrOk)
     421           0 :                 return (err);
     422           0 :             *value = (int8_t)m;
     423           0 :             return (TIFFReadDirEntryErrOk);
     424             :         }
     425           0 :         case TIFF_SSHORT:
     426             :         {
     427             :             int16_t m;
     428           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     429           0 :             err = TIFFReadDirEntryCheckRangeSbyteSshort(m);
     430           0 :             if (err != TIFFReadDirEntryErrOk)
     431           0 :                 return (err);
     432           0 :             *value = (int8_t)m;
     433           0 :             return (TIFFReadDirEntryErrOk);
     434             :         }
     435           0 :         case TIFF_LONG:
     436             :         {
     437             :             uint32_t m;
     438           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     439           0 :             err = TIFFReadDirEntryCheckRangeSbyteLong(m);
     440           0 :             if (err != TIFFReadDirEntryErrOk)
     441           0 :                 return (err);
     442           0 :             *value = (int8_t)m;
     443           0 :             return (TIFFReadDirEntryErrOk);
     444             :         }
     445           0 :         case TIFF_SLONG:
     446             :         {
     447             :             int32_t m;
     448           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     449           0 :             err = TIFFReadDirEntryCheckRangeSbyteSlong(m);
     450           0 :             if (err != TIFFReadDirEntryErrOk)
     451           0 :                 return (err);
     452           0 :             *value = (int8_t)m;
     453           0 :             return (TIFFReadDirEntryErrOk);
     454             :         }
     455           0 :         case TIFF_LONG8:
     456             :         {
     457             :             uint64_t m;
     458           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     459           0 :             if (err != TIFFReadDirEntryErrOk)
     460           0 :                 return (err);
     461           0 :             err = TIFFReadDirEntryCheckRangeSbyteLong8(m);
     462           0 :             if (err != TIFFReadDirEntryErrOk)
     463           0 :                 return (err);
     464           0 :             *value = (int8_t)m;
     465           0 :             return (TIFFReadDirEntryErrOk);
     466             :         }
     467           0 :         case TIFF_SLONG8:
     468             :         {
     469             :             int64_t m;
     470           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     471           0 :             if (err != TIFFReadDirEntryErrOk)
     472           0 :                 return (err);
     473           0 :             err = TIFFReadDirEntryCheckRangeSbyteSlong8(m);
     474           0 :             if (err != TIFFReadDirEntryErrOk)
     475           0 :                 return (err);
     476           0 :             *value = (int8_t)m;
     477           0 :             return (TIFFReadDirEntryErrOk);
     478             :         }
     479           0 :         default:
     480           0 :             return (TIFFReadDirEntryErrType);
     481             :     }
     482             : } /*-- TIFFReadDirEntrySbyte() --*/
     483             : 
     484             : static enum TIFFReadDirEntryErr
     485      329302 : TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
     486             : {
     487             :     enum TIFFReadDirEntryErr err;
     488      329302 :     if (direntry->tdir_count != 1)
     489       32705 :         return (TIFFReadDirEntryErrCount);
     490      296597 :     switch (direntry->tdir_type)
     491             :     {
     492           0 :         case TIFF_BYTE:
     493             :         {
     494             :             uint8_t m;
     495           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     496           0 :             *value = (uint16_t)m;
     497           0 :             return (TIFFReadDirEntryErrOk);
     498             :         }
     499           0 :         case TIFF_SBYTE:
     500             :         {
     501             :             int8_t m;
     502           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     503           0 :             err = TIFFReadDirEntryCheckRangeShortSbyte(m);
     504           0 :             if (err != TIFFReadDirEntryErrOk)
     505           0 :                 return (err);
     506           0 :             *value = (uint16_t)m;
     507           0 :             return (TIFFReadDirEntryErrOk);
     508             :         }
     509      296469 :         case TIFF_SHORT:
     510      296469 :             TIFFReadDirEntryCheckedShort(tif, direntry, value);
     511      296451 :             return (TIFFReadDirEntryErrOk);
     512           0 :         case TIFF_SSHORT:
     513             :         {
     514             :             int16_t m;
     515           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     516           0 :             err = TIFFReadDirEntryCheckRangeShortSshort(m);
     517           0 :             if (err != TIFFReadDirEntryErrOk)
     518           0 :                 return (err);
     519           0 :             *value = (uint16_t)m;
     520           0 :             return (TIFFReadDirEntryErrOk);
     521             :         }
     522           1 :         case TIFF_LONG:
     523             :         {
     524             :             uint32_t m;
     525           1 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     526           1 :             err = TIFFReadDirEntryCheckRangeShortLong(m);
     527           1 :             if (err != TIFFReadDirEntryErrOk)
     528           0 :                 return (err);
     529           1 :             *value = (uint16_t)m;
     530           1 :             return (TIFFReadDirEntryErrOk);
     531             :         }
     532           0 :         case TIFF_SLONG:
     533             :         {
     534             :             int32_t m;
     535           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     536           0 :             err = TIFFReadDirEntryCheckRangeShortSlong(m);
     537           0 :             if (err != TIFFReadDirEntryErrOk)
     538           0 :                 return (err);
     539           0 :             *value = (uint16_t)m;
     540           0 :             return (TIFFReadDirEntryErrOk);
     541             :         }
     542           0 :         case TIFF_LONG8:
     543             :         {
     544             :             uint64_t m;
     545           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     546           0 :             if (err != TIFFReadDirEntryErrOk)
     547           0 :                 return (err);
     548           0 :             err = TIFFReadDirEntryCheckRangeShortLong8(m);
     549           0 :             if (err != TIFFReadDirEntryErrOk)
     550           0 :                 return (err);
     551           0 :             *value = (uint16_t)m;
     552           0 :             return (TIFFReadDirEntryErrOk);
     553             :         }
     554           0 :         case TIFF_SLONG8:
     555             :         {
     556             :             int64_t m;
     557           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     558           0 :             if (err != TIFFReadDirEntryErrOk)
     559           0 :                 return (err);
     560           0 :             err = TIFFReadDirEntryCheckRangeShortSlong8(m);
     561           0 :             if (err != TIFFReadDirEntryErrOk)
     562           0 :                 return (err);
     563           0 :             *value = (uint16_t)m;
     564           0 :             return (TIFFReadDirEntryErrOk);
     565             :         }
     566         127 :         default:
     567         127 :             return (TIFFReadDirEntryErrType);
     568             :     }
     569             : } /*-- TIFFReadDirEntryShort() --*/
     570             : 
     571             : static enum TIFFReadDirEntryErr
     572           0 : TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
     573             : {
     574             :     enum TIFFReadDirEntryErr err;
     575           0 :     if (direntry->tdir_count != 1)
     576           0 :         return (TIFFReadDirEntryErrCount);
     577           0 :     switch (direntry->tdir_type)
     578             :     {
     579           0 :         case TIFF_BYTE:
     580             :         {
     581             :             uint8_t m;
     582           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     583           0 :             *value = (int16_t)m;
     584           0 :             return (TIFFReadDirEntryErrOk);
     585             :         }
     586           0 :         case TIFF_SBYTE:
     587             :         {
     588             :             int8_t m;
     589           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     590           0 :             *value = (int16_t)m;
     591           0 :             return (TIFFReadDirEntryErrOk);
     592             :         }
     593           0 :         case TIFF_SHORT:
     594             :         {
     595             :             uint16_t m;
     596           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     597           0 :             err = TIFFReadDirEntryCheckRangeSshortShort(m);
     598           0 :             if (err != TIFFReadDirEntryErrOk)
     599           0 :                 return (err);
     600           0 :             *value = (uint16_t)m;
     601           0 :             return (TIFFReadDirEntryErrOk);
     602             :         }
     603           0 :         case TIFF_SSHORT:
     604           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, value);
     605           0 :             return (TIFFReadDirEntryErrOk);
     606           0 :         case TIFF_LONG:
     607             :         {
     608             :             uint32_t m;
     609           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     610           0 :             err = TIFFReadDirEntryCheckRangeSshortLong(m);
     611           0 :             if (err != TIFFReadDirEntryErrOk)
     612           0 :                 return (err);
     613           0 :             *value = (int16_t)m;
     614           0 :             return (TIFFReadDirEntryErrOk);
     615             :         }
     616           0 :         case TIFF_SLONG:
     617             :         {
     618             :             int32_t m;
     619           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     620           0 :             err = TIFFReadDirEntryCheckRangeSshortSlong(m);
     621           0 :             if (err != TIFFReadDirEntryErrOk)
     622           0 :                 return (err);
     623           0 :             *value = (int16_t)m;
     624           0 :             return (TIFFReadDirEntryErrOk);
     625             :         }
     626           0 :         case TIFF_LONG8:
     627             :         {
     628             :             uint64_t m;
     629           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     630           0 :             if (err != TIFFReadDirEntryErrOk)
     631           0 :                 return (err);
     632           0 :             err = TIFFReadDirEntryCheckRangeSshortLong8(m);
     633           0 :             if (err != TIFFReadDirEntryErrOk)
     634           0 :                 return (err);
     635           0 :             *value = (int16_t)m;
     636           0 :             return (TIFFReadDirEntryErrOk);
     637             :         }
     638           0 :         case TIFF_SLONG8:
     639             :         {
     640             :             int64_t m;
     641           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     642           0 :             if (err != TIFFReadDirEntryErrOk)
     643           0 :                 return (err);
     644           0 :             err = TIFFReadDirEntryCheckRangeSshortSlong8(m);
     645           0 :             if (err != TIFFReadDirEntryErrOk)
     646           0 :                 return (err);
     647           0 :             *value = (int16_t)m;
     648           0 :             return (TIFFReadDirEntryErrOk);
     649             :         }
     650           0 :         default:
     651           0 :             return (TIFFReadDirEntryErrType);
     652             :     }
     653             : } /*-- TIFFReadDirEntrySshort() --*/
     654             : 
     655             : static enum TIFFReadDirEntryErr
     656      177166 : TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
     657             : {
     658             :     enum TIFFReadDirEntryErr err;
     659      177166 :     if (direntry->tdir_count != 1)
     660           0 :         return (TIFFReadDirEntryErrCount);
     661      177166 :     switch (direntry->tdir_type)
     662             :     {
     663           0 :         case TIFF_BYTE:
     664             :         {
     665             :             uint8_t m;
     666           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     667           0 :             *value = (uint32_t)m;
     668           0 :             return (TIFFReadDirEntryErrOk);
     669             :         }
     670           0 :         case TIFF_SBYTE:
     671             :         {
     672             :             int8_t m;
     673           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     674           0 :             err = TIFFReadDirEntryCheckRangeLongSbyte(m);
     675           0 :             if (err != TIFFReadDirEntryErrOk)
     676           0 :                 return (err);
     677           0 :             *value = (uint32_t)m;
     678           0 :             return (TIFFReadDirEntryErrOk);
     679             :         }
     680      171226 :         case TIFF_SHORT:
     681             :         {
     682             :             uint16_t m;
     683      171226 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     684      171260 :             *value = (uint32_t)m;
     685      171260 :             return (TIFFReadDirEntryErrOk);
     686             :         }
     687           0 :         case TIFF_SSHORT:
     688             :         {
     689             :             int16_t m;
     690           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     691           0 :             err = TIFFReadDirEntryCheckRangeLongSshort(m);
     692           0 :             if (err != TIFFReadDirEntryErrOk)
     693           0 :                 return (err);
     694           0 :             *value = (uint32_t)m;
     695           0 :             return (TIFFReadDirEntryErrOk);
     696             :         }
     697        5866 :         case TIFF_LONG:
     698             :         case TIFF_IFD:
     699        5866 :             TIFFReadDirEntryCheckedLong(tif, direntry, value);
     700        5867 :             return (TIFFReadDirEntryErrOk);
     701           0 :         case TIFF_SLONG:
     702             :         {
     703             :             int32_t m;
     704           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     705           0 :             err = TIFFReadDirEntryCheckRangeLongSlong(m);
     706           0 :             if (err != TIFFReadDirEntryErrOk)
     707           0 :                 return (err);
     708           0 :             *value = (uint32_t)m;
     709           0 :             return (TIFFReadDirEntryErrOk);
     710             :         }
     711           0 :         case TIFF_LONG8:
     712             :         case TIFF_IFD8:
     713             :         {
     714             :             uint64_t m;
     715           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     716           0 :             if (err != TIFFReadDirEntryErrOk)
     717           0 :                 return (err);
     718           0 :             err = TIFFReadDirEntryCheckRangeLongLong8(m);
     719           0 :             if (err != TIFFReadDirEntryErrOk)
     720           0 :                 return (err);
     721           0 :             *value = (uint32_t)m;
     722           0 :             return (TIFFReadDirEntryErrOk);
     723             :         }
     724           0 :         case TIFF_SLONG8:
     725             :         {
     726             :             int64_t m;
     727           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     728           0 :             if (err != TIFFReadDirEntryErrOk)
     729           0 :                 return (err);
     730           0 :             err = TIFFReadDirEntryCheckRangeLongSlong8(m);
     731           0 :             if (err != TIFFReadDirEntryErrOk)
     732           0 :                 return (err);
     733           0 :             *value = (uint32_t)m;
     734           0 :             return (TIFFReadDirEntryErrOk);
     735             :         }
     736          74 :         default:
     737          74 :             return (TIFFReadDirEntryErrType);
     738             :     }
     739             : } /*-- TIFFReadDirEntryLong() --*/
     740             : 
     741             : static enum TIFFReadDirEntryErr
     742           0 : TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
     743             : {
     744             :     enum TIFFReadDirEntryErr err;
     745           0 :     if (direntry->tdir_count != 1)
     746           0 :         return (TIFFReadDirEntryErrCount);
     747           0 :     switch (direntry->tdir_type)
     748             :     {
     749           0 :         case TIFF_BYTE:
     750             :         {
     751             :             uint8_t m;
     752           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     753           0 :             *value = (int32_t)m;
     754           0 :             return (TIFFReadDirEntryErrOk);
     755             :         }
     756           0 :         case TIFF_SBYTE:
     757             :         {
     758             :             int8_t m;
     759           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     760           0 :             *value = (int32_t)m;
     761           0 :             return (TIFFReadDirEntryErrOk);
     762             :         }
     763           0 :         case TIFF_SHORT:
     764             :         {
     765             :             uint16_t m;
     766           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     767           0 :             *value = (int32_t)m;
     768           0 :             return (TIFFReadDirEntryErrOk);
     769             :         }
     770           0 :         case TIFF_SSHORT:
     771             :         {
     772             :             int16_t m;
     773           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     774           0 :             *value = (int32_t)m;
     775           0 :             return (TIFFReadDirEntryErrOk);
     776             :         }
     777           0 :         case TIFF_LONG:
     778             :         {
     779             :             uint32_t m;
     780           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     781           0 :             err = TIFFReadDirEntryCheckRangeSlongLong(m);
     782           0 :             if (err != TIFFReadDirEntryErrOk)
     783           0 :                 return (err);
     784           0 :             *value = (int32_t)m;
     785           0 :             return (TIFFReadDirEntryErrOk);
     786             :         }
     787           0 :         case TIFF_SLONG:
     788           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, value);
     789           0 :             return (TIFFReadDirEntryErrOk);
     790           0 :         case TIFF_LONG8:
     791             :         {
     792             :             uint64_t m;
     793           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     794           0 :             if (err != TIFFReadDirEntryErrOk)
     795           0 :                 return (err);
     796           0 :             err = TIFFReadDirEntryCheckRangeSlongLong8(m);
     797           0 :             if (err != TIFFReadDirEntryErrOk)
     798           0 :                 return (err);
     799           0 :             *value = (int32_t)m;
     800           0 :             return (TIFFReadDirEntryErrOk);
     801             :         }
     802           0 :         case TIFF_SLONG8:
     803             :         {
     804             :             int64_t m;
     805           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     806           0 :             if (err != TIFFReadDirEntryErrOk)
     807           0 :                 return (err);
     808           0 :             err = TIFFReadDirEntryCheckRangeSlongSlong8(m);
     809           0 :             if (err != TIFFReadDirEntryErrOk)
     810           0 :                 return (err);
     811           0 :             *value = (int32_t)m;
     812           0 :             return (TIFFReadDirEntryErrOk);
     813             :         }
     814           0 :         default:
     815           0 :             return (TIFFReadDirEntryErrType);
     816             :     }
     817             : } /*-- TIFFReadDirEntrySlong() --*/
     818             : 
     819             : static enum TIFFReadDirEntryErr
     820           8 : TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
     821             : {
     822             :     enum TIFFReadDirEntryErr err;
     823           8 :     if (direntry->tdir_count != 1)
     824           0 :         return (TIFFReadDirEntryErrCount);
     825           8 :     switch (direntry->tdir_type)
     826             :     {
     827           0 :         case TIFF_BYTE:
     828             :         {
     829             :             uint8_t m;
     830           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     831           0 :             *value = (uint64_t)m;
     832           0 :             return (TIFFReadDirEntryErrOk);
     833             :         }
     834           0 :         case TIFF_SBYTE:
     835             :         {
     836             :             int8_t m;
     837           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     838           0 :             err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
     839           0 :             if (err != TIFFReadDirEntryErrOk)
     840           0 :                 return (err);
     841           0 :             *value = (uint64_t)m;
     842           0 :             return (TIFFReadDirEntryErrOk);
     843             :         }
     844           0 :         case TIFF_SHORT:
     845             :         {
     846             :             uint16_t m;
     847           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     848           0 :             *value = (uint64_t)m;
     849           0 :             return (TIFFReadDirEntryErrOk);
     850             :         }
     851           0 :         case TIFF_SSHORT:
     852             :         {
     853             :             int16_t m;
     854           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     855           0 :             err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
     856           0 :             if (err != TIFFReadDirEntryErrOk)
     857           0 :                 return (err);
     858           0 :             *value = (uint64_t)m;
     859           0 :             return (TIFFReadDirEntryErrOk);
     860             :         }
     861           8 :         case TIFF_LONG:
     862             :         case TIFF_IFD:
     863             :         {
     864             :             uint32_t m;
     865           8 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     866           8 :             *value = (uint64_t)m;
     867           8 :             return (TIFFReadDirEntryErrOk);
     868             :         }
     869           0 :         case TIFF_SLONG:
     870             :         {
     871             :             int32_t m;
     872           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     873           0 :             err = TIFFReadDirEntryCheckRangeLong8Slong(m);
     874           0 :             if (err != TIFFReadDirEntryErrOk)
     875           0 :                 return (err);
     876           0 :             *value = (uint64_t)m;
     877           0 :             return (TIFFReadDirEntryErrOk);
     878             :         }
     879           0 :         case TIFF_LONG8:
     880             :         case TIFF_IFD8:
     881           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
     882           0 :             return (err);
     883           0 :         case TIFF_SLONG8:
     884             :         {
     885             :             int64_t m;
     886           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
     887           0 :             if (err != TIFFReadDirEntryErrOk)
     888           0 :                 return (err);
     889           0 :             err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
     890           0 :             if (err != TIFFReadDirEntryErrOk)
     891           0 :                 return (err);
     892           0 :             *value = (uint64_t)m;
     893           0 :             return (TIFFReadDirEntryErrOk);
     894             :         }
     895           0 :         default:
     896           0 :             return (TIFFReadDirEntryErrType);
     897             :     }
     898             : } /*-- TIFFReadDirEntryLong8() --*/
     899             : 
     900             : static enum TIFFReadDirEntryErr
     901           0 : TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
     902             : {
     903             :     enum TIFFReadDirEntryErr err;
     904           0 :     if (direntry->tdir_count != 1)
     905           0 :         return (TIFFReadDirEntryErrCount);
     906           0 :     switch (direntry->tdir_type)
     907             :     {
     908           0 :         case TIFF_BYTE:
     909             :         {
     910             :             uint8_t m;
     911           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     912           0 :             *value = (int64_t)m;
     913           0 :             return (TIFFReadDirEntryErrOk);
     914             :         }
     915           0 :         case TIFF_SBYTE:
     916             :         {
     917             :             int8_t m;
     918           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     919           0 :             *value = (int64_t)m;
     920           0 :             return (TIFFReadDirEntryErrOk);
     921             :         }
     922           0 :         case TIFF_SHORT:
     923             :         {
     924             :             uint16_t m;
     925           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     926           0 :             *value = (int64_t)m;
     927           0 :             return (TIFFReadDirEntryErrOk);
     928             :         }
     929           0 :         case TIFF_SSHORT:
     930             :         {
     931             :             int16_t m;
     932           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
     933           0 :             *value = (int64_t)m;
     934           0 :             return (TIFFReadDirEntryErrOk);
     935             :         }
     936           0 :         case TIFF_LONG:
     937             :         {
     938             :             uint32_t m;
     939           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
     940           0 :             *value = (int64_t)m;
     941           0 :             return (TIFFReadDirEntryErrOk);
     942             :         }
     943           0 :         case TIFF_SLONG:
     944             :         {
     945             :             int32_t m;
     946           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
     947           0 :             *value = (int64_t)m;
     948           0 :             return (TIFFReadDirEntryErrOk);
     949             :         }
     950           0 :         case TIFF_LONG8:
     951             :         {
     952             :             uint64_t m;
     953           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
     954           0 :             if (err != TIFFReadDirEntryErrOk)
     955           0 :                 return (err);
     956           0 :             err = TIFFReadDirEntryCheckRangeSlong8Long8(m);
     957           0 :             if (err != TIFFReadDirEntryErrOk)
     958           0 :                 return (err);
     959           0 :             *value = (int64_t)m;
     960           0 :             return (TIFFReadDirEntryErrOk);
     961             :         }
     962           0 :         case TIFF_SLONG8:
     963           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
     964           0 :             return (err);
     965           0 :         default:
     966           0 :             return (TIFFReadDirEntryErrType);
     967             :     }
     968             : } /*-- TIFFReadDirEntrySlong8() --*/
     969             : 
     970             : static enum TIFFReadDirEntryErr
     971         248 : TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
     972             : {
     973             :     enum TIFFReadDirEntryErr err;
     974         248 :     if (direntry->tdir_count != 1)
     975           0 :         return (TIFFReadDirEntryErrCount);
     976         248 :     switch (direntry->tdir_type)
     977             :     {
     978           0 :         case TIFF_BYTE:
     979             :         {
     980             :             uint8_t m;
     981           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
     982           0 :             *value = (float)m;
     983           0 :             return (TIFFReadDirEntryErrOk);
     984             :         }
     985           0 :         case TIFF_SBYTE:
     986             :         {
     987             :             int8_t m;
     988           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
     989           0 :             *value = (float)m;
     990           0 :             return (TIFFReadDirEntryErrOk);
     991             :         }
     992           0 :         case TIFF_SHORT:
     993             :         {
     994             :             uint16_t m;
     995           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
     996           0 :             *value = (float)m;
     997           0 :             return (TIFFReadDirEntryErrOk);
     998             :         }
     999           0 :         case TIFF_SSHORT:
    1000             :         {
    1001             :             int16_t m;
    1002           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
    1003           0 :             *value = (float)m;
    1004           0 :             return (TIFFReadDirEntryErrOk);
    1005             :         }
    1006           0 :         case TIFF_LONG:
    1007             :         {
    1008             :             uint32_t m;
    1009           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
    1010           0 :             *value = (float)m;
    1011           0 :             return (TIFFReadDirEntryErrOk);
    1012             :         }
    1013           0 :         case TIFF_SLONG:
    1014             :         {
    1015             :             int32_t m;
    1016           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
    1017           0 :             *value = (float)m;
    1018           0 :             return (TIFFReadDirEntryErrOk);
    1019             :         }
    1020           0 :         case TIFF_LONG8:
    1021             :         {
    1022             :             uint64_t m;
    1023           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
    1024           0 :             if (err != TIFFReadDirEntryErrOk)
    1025           0 :                 return (err);
    1026           0 :             *value = (float)m;
    1027           0 :             return (TIFFReadDirEntryErrOk);
    1028             :         }
    1029           0 :         case TIFF_SLONG8:
    1030             :         {
    1031             :             int64_t m;
    1032           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
    1033           0 :             if (err != TIFFReadDirEntryErrOk)
    1034           0 :                 return (err);
    1035           0 :             *value = (float)m;
    1036           0 :             return (TIFFReadDirEntryErrOk);
    1037             :         }
    1038         248 :         case TIFF_RATIONAL:
    1039             :         {
    1040             :             double m;
    1041         248 :             err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
    1042         248 :             if (err != TIFFReadDirEntryErrOk)
    1043           0 :                 return (err);
    1044         248 :             *value = (float)m;
    1045         248 :             return (TIFFReadDirEntryErrOk);
    1046             :         }
    1047           0 :         case TIFF_SRATIONAL:
    1048             :         {
    1049             :             double m;
    1050           0 :             err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
    1051           0 :             if (err != TIFFReadDirEntryErrOk)
    1052           0 :                 return (err);
    1053           0 :             *value = (float)m;
    1054           0 :             return (TIFFReadDirEntryErrOk);
    1055             :         }
    1056           0 :         case TIFF_FLOAT:
    1057           0 :             TIFFReadDirEntryCheckedFloat(tif, direntry, value);
    1058           0 :             return (TIFFReadDirEntryErrOk);
    1059           0 :         case TIFF_DOUBLE:
    1060             :         {
    1061             :             double m;
    1062           0 :             err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
    1063           0 :             if (err != TIFFReadDirEntryErrOk)
    1064           0 :                 return (err);
    1065           0 :             if ((m > FLT_MAX) || (m < -FLT_MAX))
    1066           0 :                 return (TIFFReadDirEntryErrRange);
    1067           0 :             *value = (float)m;
    1068           0 :             return (TIFFReadDirEntryErrOk);
    1069             :         }
    1070           0 :         default:
    1071           0 :             return (TIFFReadDirEntryErrType);
    1072             :     }
    1073             : }
    1074             : 
    1075             : static enum TIFFReadDirEntryErr
    1076           0 : TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
    1077             : {
    1078             :     enum TIFFReadDirEntryErr err;
    1079           0 :     if (direntry->tdir_count != 1)
    1080           0 :         return (TIFFReadDirEntryErrCount);
    1081           0 :     switch (direntry->tdir_type)
    1082             :     {
    1083           0 :         case TIFF_BYTE:
    1084             :         {
    1085             :             uint8_t m;
    1086           0 :             TIFFReadDirEntryCheckedByte(tif, direntry, &m);
    1087           0 :             *value = (double)m;
    1088           0 :             return (TIFFReadDirEntryErrOk);
    1089             :         }
    1090           0 :         case TIFF_SBYTE:
    1091             :         {
    1092             :             int8_t m;
    1093           0 :             TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
    1094           0 :             *value = (double)m;
    1095           0 :             return (TIFFReadDirEntryErrOk);
    1096             :         }
    1097           0 :         case TIFF_SHORT:
    1098             :         {
    1099             :             uint16_t m;
    1100           0 :             TIFFReadDirEntryCheckedShort(tif, direntry, &m);
    1101           0 :             *value = (double)m;
    1102           0 :             return (TIFFReadDirEntryErrOk);
    1103             :         }
    1104           0 :         case TIFF_SSHORT:
    1105             :         {
    1106             :             int16_t m;
    1107           0 :             TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
    1108           0 :             *value = (double)m;
    1109           0 :             return (TIFFReadDirEntryErrOk);
    1110             :         }
    1111           0 :         case TIFF_LONG:
    1112             :         {
    1113             :             uint32_t m;
    1114           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
    1115           0 :             *value = (double)m;
    1116           0 :             return (TIFFReadDirEntryErrOk);
    1117             :         }
    1118           0 :         case TIFF_SLONG:
    1119             :         {
    1120             :             int32_t m;
    1121           0 :             TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
    1122           0 :             *value = (double)m;
    1123           0 :             return (TIFFReadDirEntryErrOk);
    1124             :         }
    1125           0 :         case TIFF_LONG8:
    1126             :         {
    1127             :             uint64_t m;
    1128           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
    1129           0 :             if (err != TIFFReadDirEntryErrOk)
    1130           0 :                 return (err);
    1131           0 :             *value = (double)m;
    1132           0 :             return (TIFFReadDirEntryErrOk);
    1133             :         }
    1134           0 :         case TIFF_SLONG8:
    1135             :         {
    1136             :             int64_t m;
    1137           0 :             err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
    1138           0 :             if (err != TIFFReadDirEntryErrOk)
    1139           0 :                 return (err);
    1140           0 :             *value = (double)m;
    1141           0 :             return (TIFFReadDirEntryErrOk);
    1142             :         }
    1143           0 :         case TIFF_RATIONAL:
    1144           0 :             err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
    1145           0 :             return (err);
    1146           0 :         case TIFF_SRATIONAL:
    1147           0 :             err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
    1148           0 :             return (err);
    1149           0 :         case TIFF_FLOAT:
    1150             :         {
    1151             :             float m;
    1152           0 :             TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
    1153           0 :             *value = (double)m;
    1154           0 :             return (TIFFReadDirEntryErrOk);
    1155             :         }
    1156           0 :         case TIFF_DOUBLE:
    1157           0 :             err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
    1158           0 :             return (err);
    1159           0 :         default:
    1160           0 :             return (TIFFReadDirEntryErrType);
    1161             :     }
    1162             : }
    1163             : 
    1164             : static enum TIFFReadDirEntryErr
    1165           0 : TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
    1166             : {
    1167             :     enum TIFFReadDirEntryErr err;
    1168           0 :     if (direntry->tdir_count != 1)
    1169           0 :         return (TIFFReadDirEntryErrCount);
    1170           0 :     switch (direntry->tdir_type)
    1171             :     {
    1172           0 :         case TIFF_LONG:
    1173             :         case TIFF_IFD:
    1174             :         {
    1175             :             uint32_t m;
    1176           0 :             TIFFReadDirEntryCheckedLong(tif, direntry, &m);
    1177           0 :             *value = (uint64_t)m;
    1178           0 :             return (TIFFReadDirEntryErrOk);
    1179             :         }
    1180           0 :         case TIFF_LONG8:
    1181             :         case TIFF_IFD8:
    1182           0 :             err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
    1183           0 :             return (err);
    1184           0 :         default:
    1185           0 :             return (TIFFReadDirEntryErrType);
    1186             :     }
    1187             : }
    1188             : 
    1189             : #define INITIAL_THRESHOLD (1024 * 1024)
    1190             : #define THRESHOLD_MULTIPLIER 10
    1191             : #define MAX_THRESHOLD                                                          \
    1192             :     (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
    1193             :      INITIAL_THRESHOLD)
    1194             : 
    1195      181981 : static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
    1196             :                                                                uint64_t offset,
    1197             :                                                                tmsize_t size,
    1198             :                                                                void **pdest)
    1199             : {
    1200             : #if SIZEOF_SIZE_T == 8
    1201      181981 :     tmsize_t threshold = INITIAL_THRESHOLD;
    1202             : #endif
    1203      181981 :     tmsize_t already_read = 0;
    1204             : 
    1205      181981 :     assert(!isMapped(tif));
    1206             : 
    1207      181981 :     if (!SeekOK(tif, offset))
    1208           0 :         return (TIFFReadDirEntryErrIo);
    1209             : 
    1210             :     /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
    1211             :     /* so as to avoid allocating too much memory in case the file is too */
    1212             :     /* short. We could ask for the file size, but this might be */
    1213             :     /* expensive with some I/O layers (think of reading a gzipped file) */
    1214             :     /* Restrict to 64 bit processes, so as to avoid reallocs() */
    1215             :     /* on 32 bit processes where virtual memory is scarce.  */
    1216      364143 :     while (already_read < size)
    1217             :     {
    1218             :         void *new_dest;
    1219             :         tmsize_t bytes_read;
    1220      182103 :         tmsize_t to_read = size - already_read;
    1221             : #if SIZEOF_SIZE_T == 8
    1222      182103 :         if (to_read >= threshold && threshold < MAX_THRESHOLD)
    1223             :         {
    1224          18 :             to_read = threshold;
    1225          18 :             threshold *= THRESHOLD_MULTIPLIER;
    1226             :         }
    1227             : #endif
    1228             : 
    1229             :         new_dest =
    1230      182103 :             (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
    1231      182063 :         if (new_dest == NULL)
    1232             :         {
    1233           0 :             TIFFErrorExtR(tif, tif->tif_name,
    1234             :                           "Failed to allocate memory for %s "
    1235             :                           "(%" TIFF_SSIZE_FORMAT
    1236             :                           " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
    1237             :                           "TIFFReadDirEntryArray", (tmsize_t)1,
    1238             :                           already_read + to_read);
    1239           0 :             return TIFFReadDirEntryErrAlloc;
    1240             :         }
    1241      182063 :         *pdest = new_dest;
    1242             : 
    1243      182063 :         bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
    1244      182064 :         already_read += bytes_read;
    1245      182064 :         if (bytes_read != to_read)
    1246             :         {
    1247          21 :             return TIFFReadDirEntryErrIo;
    1248             :         }
    1249             :     }
    1250      182040 :     return TIFFReadDirEntryErrOk;
    1251             : }
    1252             : 
    1253             : /* Caution: if raising that value, make sure int32 / uint32 overflows can't
    1254             :  * occur elsewhere */
    1255             : #define MAX_SIZE_TAG_DATA 2147483647U
    1256             : 
    1257             : static enum TIFFReadDirEntryErr
    1258      265766 : TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
    1259             :                                uint32_t *count, uint32_t desttypesize,
    1260             :                                void **value, uint64_t maxcount)
    1261             : {
    1262             :     int typesize;
    1263             :     uint32_t datasize;
    1264             :     void *data;
    1265             :     uint64_t target_count64;
    1266             :     int original_datasize_clamped;
    1267      265766 :     typesize = TIFFDataWidth((TIFFDataType)direntry->tdir_type);
    1268             : 
    1269      265613 :     target_count64 =
    1270      265613 :         (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
    1271             : 
    1272      265613 :     if ((target_count64 == 0) || (typesize == 0))
    1273             :     {
    1274           0 :         *value = 0;
    1275           0 :         return (TIFFReadDirEntryErrOk);
    1276             :     }
    1277             :     (void)desttypesize;
    1278             : 
    1279             :     /* We just want to know if the original tag size is more than 4 bytes
    1280             :      * (classic TIFF) or 8 bytes (BigTIFF)
    1281             :      */
    1282      265697 :     original_datasize_clamped =
    1283      265697 :         ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
    1284             :         typesize;
    1285             : 
    1286             :     /*
    1287             :      * As a sanity check, make sure we have no more than a 2GB tag array
    1288             :      * in either the current data type or the dest data type.  This also
    1289             :      * avoids problems with overflow of tmsize_t on 32bit systems.
    1290             :      */
    1291      265697 :     if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64)
    1292           3 :         return (TIFFReadDirEntryErrSizesan);
    1293      265694 :     if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
    1294           0 :         return (TIFFReadDirEntryErrSizesan);
    1295             : 
    1296      265694 :     *count = (uint32_t)target_count64;
    1297      265694 :     datasize = (*count) * typesize;
    1298      265694 :     assert((tmsize_t)datasize > 0);
    1299             : 
    1300      265694 :     if (datasize > 100 * 1024 * 1024)
    1301             :     {
    1302             :         /* Before allocating a huge amount of memory for corrupted files, check
    1303             :          * if size of requested memory is not greater than file size.
    1304             :          */
    1305          15 :         const uint64_t filesize = TIFFGetFileSize(tif);
    1306          15 :         if (datasize > filesize)
    1307             :         {
    1308          15 :             TIFFWarningExtR(tif, "ReadDirEntryArray",
    1309             :                             "Requested memory size for tag %d (0x%x) %" PRIu32
    1310             :                             " is greater than filesize %" PRIu64
    1311             :                             ". Memory not allocated, tag not read",
    1312          15 :                             direntry->tdir_tag, direntry->tdir_tag, datasize,
    1313             :                             filesize);
    1314          15 :             return (TIFFReadDirEntryErrAlloc);
    1315             :         }
    1316             :     }
    1317             : 
    1318      265679 :     if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
    1319           0 :         return TIFFReadDirEntryErrIo;
    1320             : 
    1321      265679 :     if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
    1322      264734 :                            (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
    1323             :     {
    1324      181856 :         data = NULL;
    1325             :     }
    1326             :     else
    1327             :     {
    1328       83823 :         data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
    1329       83805 :         if (data == 0)
    1330           0 :             return (TIFFReadDirEntryErrAlloc);
    1331             :     }
    1332      265661 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    1333             :     {
    1334             :         /* Only the condition on original_datasize_clamped. The second
    1335             :          * one is implied, but Coverity Scan cannot see it. */
    1336      263370 :         if (original_datasize_clamped <= 4 && datasize <= 4)
    1337       82476 :             _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
    1338             :         else
    1339             :         {
    1340             :             enum TIFFReadDirEntryErr err;
    1341      180894 :             uint32_t offset = direntry->tdir_offset.toff_long;
    1342      180894 :             if (tif->tif_flags & TIFF_SWAB)
    1343        1941 :                 TIFFSwabLong(&offset);
    1344      180965 :             if (isMapped(tif))
    1345          32 :                 err = TIFFReadDirEntryData(tif, (uint64_t)offset,
    1346             :                                            (tmsize_t)datasize, data);
    1347             :             else
    1348      180933 :                 err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
    1349             :                                                      (tmsize_t)datasize, &data);
    1350      181087 :             if (err != TIFFReadDirEntryErrOk)
    1351             :             {
    1352          21 :                 _TIFFfreeExt(tif, data);
    1353          21 :                 return (err);
    1354             :             }
    1355             :         }
    1356             :     }
    1357             :     else
    1358             :     {
    1359             :         /* See above comment for the Classic TIFF case */
    1360        2291 :         if (original_datasize_clamped <= 8 && datasize <= 8)
    1361        1287 :             _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
    1362             :         else
    1363             :         {
    1364             :             enum TIFFReadDirEntryErr err;
    1365        1004 :             uint64_t offset = direntry->tdir_offset.toff_long8;
    1366        1004 :             if (tif->tif_flags & TIFF_SWAB)
    1367          14 :                 TIFFSwabLong8(&offset);
    1368         959 :             if (isMapped(tif))
    1369           0 :                 err = TIFFReadDirEntryData(tif, (uint64_t)offset,
    1370             :                                            (tmsize_t)datasize, data);
    1371             :             else
    1372         959 :                 err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
    1373             :                                                      (tmsize_t)datasize, &data);
    1374         959 :             if (err != TIFFReadDirEntryErrOk)
    1375             :             {
    1376           0 :                 _TIFFfreeExt(tif, data);
    1377           0 :                 return (err);
    1378             :             }
    1379             :         }
    1380             :     }
    1381      265784 :     *value = data;
    1382      265784 :     return (TIFFReadDirEntryErrOk);
    1383             : }
    1384             : 
    1385             : static enum TIFFReadDirEntryErr
    1386      181170 : TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
    1387             :                       uint32_t desttypesize, void **value)
    1388             : {
    1389      181170 :     return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
    1390             :                                           value, ~((uint64_t)0));
    1391             : }
    1392             : 
    1393             : static enum TIFFReadDirEntryErr
    1394       40440 : TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
    1395             : {
    1396             :     enum TIFFReadDirEntryErr err;
    1397             :     uint32_t count;
    1398             :     void *origdata;
    1399             :     uint8_t *data;
    1400       40440 :     switch (direntry->tdir_type)
    1401             :     {
    1402       40426 :         case TIFF_ASCII:
    1403             :         case TIFF_UNDEFINED:
    1404             :         case TIFF_BYTE:
    1405             :         case TIFF_SBYTE:
    1406             :         case TIFF_SHORT:
    1407             :         case TIFF_SSHORT:
    1408             :         case TIFF_LONG:
    1409             :         case TIFF_SLONG:
    1410             :         case TIFF_LONG8:
    1411             :         case TIFF_SLONG8:
    1412       40426 :             break;
    1413          14 :         default:
    1414          14 :             return (TIFFReadDirEntryErrType);
    1415             :     }
    1416       40426 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
    1417       40423 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1418             :     {
    1419          66 :         *value = 0;
    1420          66 :         return (err);
    1421             :     }
    1422       40357 :     switch (direntry->tdir_type)
    1423             :     {
    1424       40352 :         case TIFF_ASCII:
    1425             :         case TIFF_UNDEFINED:
    1426             :         case TIFF_BYTE:
    1427       40352 :             *value = (uint8_t *)origdata;
    1428       40352 :             return (TIFFReadDirEntryErrOk);
    1429           0 :         case TIFF_SBYTE:
    1430             :         {
    1431             :             int8_t *m;
    1432             :             uint32_t n;
    1433           0 :             m = (int8_t *)origdata;
    1434           0 :             for (n = 0; n < count; n++)
    1435             :             {
    1436           0 :                 err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
    1437           0 :                 if (err != TIFFReadDirEntryErrOk)
    1438             :                 {
    1439           0 :                     _TIFFfreeExt(tif, origdata);
    1440           0 :                     return (err);
    1441             :                 }
    1442           0 :                 m++;
    1443             :             }
    1444           0 :             *value = (uint8_t *)origdata;
    1445           0 :             return (TIFFReadDirEntryErrOk);
    1446             :         }
    1447           5 :         default:
    1448           5 :             break;
    1449             :     }
    1450           5 :     data = (uint8_t *)_TIFFmallocExt(tif, count);
    1451           0 :     if (data == 0)
    1452             :     {
    1453           0 :         _TIFFfreeExt(tif, origdata);
    1454           0 :         return (TIFFReadDirEntryErrAlloc);
    1455             :     }
    1456           0 :     switch (direntry->tdir_type)
    1457             :     {
    1458           0 :         case TIFF_SHORT:
    1459             :         {
    1460             :             uint16_t *ma;
    1461             :             uint8_t *mb;
    1462             :             uint32_t n;
    1463           0 :             ma = (uint16_t *)origdata;
    1464           0 :             mb = data;
    1465           0 :             for (n = 0; n < count; n++)
    1466             :             {
    1467           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1468           0 :                     TIFFSwabShort(ma);
    1469           0 :                 err = TIFFReadDirEntryCheckRangeByteShort(*ma);
    1470           0 :                 if (err != TIFFReadDirEntryErrOk)
    1471           0 :                     break;
    1472           0 :                 *mb++ = (uint8_t)(*ma++);
    1473             :             }
    1474             :         }
    1475           0 :         break;
    1476           0 :         case TIFF_SSHORT:
    1477             :         {
    1478             :             int16_t *ma;
    1479             :             uint8_t *mb;
    1480             :             uint32_t n;
    1481           0 :             ma = (int16_t *)origdata;
    1482           0 :             mb = data;
    1483           0 :             for (n = 0; n < count; n++)
    1484             :             {
    1485           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1486           0 :                     TIFFSwabShort((uint16_t *)ma);
    1487           0 :                 err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
    1488           0 :                 if (err != TIFFReadDirEntryErrOk)
    1489           0 :                     break;
    1490           0 :                 *mb++ = (uint8_t)(*ma++);
    1491             :             }
    1492             :         }
    1493           0 :         break;
    1494           0 :         case TIFF_LONG:
    1495             :         {
    1496             :             uint32_t *ma;
    1497             :             uint8_t *mb;
    1498             :             uint32_t n;
    1499           0 :             ma = (uint32_t *)origdata;
    1500           0 :             mb = data;
    1501           0 :             for (n = 0; n < count; n++)
    1502             :             {
    1503           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1504           0 :                     TIFFSwabLong(ma);
    1505           0 :                 err = TIFFReadDirEntryCheckRangeByteLong(*ma);
    1506           0 :                 if (err != TIFFReadDirEntryErrOk)
    1507           0 :                     break;
    1508           0 :                 *mb++ = (uint8_t)(*ma++);
    1509             :             }
    1510             :         }
    1511           0 :         break;
    1512           0 :         case TIFF_SLONG:
    1513             :         {
    1514             :             int32_t *ma;
    1515             :             uint8_t *mb;
    1516             :             uint32_t n;
    1517           0 :             ma = (int32_t *)origdata;
    1518           0 :             mb = data;
    1519           0 :             for (n = 0; n < count; n++)
    1520             :             {
    1521           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1522           0 :                     TIFFSwabLong((uint32_t *)ma);
    1523           0 :                 err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
    1524           0 :                 if (err != TIFFReadDirEntryErrOk)
    1525           0 :                     break;
    1526           0 :                 *mb++ = (uint8_t)(*ma++);
    1527             :             }
    1528             :         }
    1529           0 :         break;
    1530           0 :         case TIFF_LONG8:
    1531             :         {
    1532             :             uint64_t *ma;
    1533             :             uint8_t *mb;
    1534             :             uint32_t n;
    1535           0 :             ma = (uint64_t *)origdata;
    1536           0 :             mb = data;
    1537           0 :             for (n = 0; n < count; n++)
    1538             :             {
    1539           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1540           0 :                     TIFFSwabLong8(ma);
    1541           0 :                 err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
    1542           0 :                 if (err != TIFFReadDirEntryErrOk)
    1543           0 :                     break;
    1544           0 :                 *mb++ = (uint8_t)(*ma++);
    1545             :             }
    1546             :         }
    1547           0 :         break;
    1548           0 :         case TIFF_SLONG8:
    1549             :         {
    1550             :             int64_t *ma;
    1551             :             uint8_t *mb;
    1552             :             uint32_t n;
    1553           0 :             ma = (int64_t *)origdata;
    1554           0 :             mb = data;
    1555           0 :             for (n = 0; n < count; n++)
    1556             :             {
    1557           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1558           0 :                     TIFFSwabLong8((uint64_t *)ma);
    1559           0 :                 err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
    1560           0 :                 if (err != TIFFReadDirEntryErrOk)
    1561           0 :                     break;
    1562           0 :                 *mb++ = (uint8_t)(*ma++);
    1563             :             }
    1564             :         }
    1565           0 :         break;
    1566           0 :         default:
    1567           0 :             break;
    1568             :     }
    1569           0 :     _TIFFfreeExt(tif, origdata);
    1570           0 :     if (err != TIFFReadDirEntryErrOk)
    1571             :     {
    1572           0 :         _TIFFfreeExt(tif, data);
    1573           0 :         return (err);
    1574             :     }
    1575           0 :     *value = data;
    1576           0 :     return (TIFFReadDirEntryErrOk);
    1577             : }
    1578             : 
    1579             : static enum TIFFReadDirEntryErr
    1580           0 : TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
    1581             : {
    1582             :     enum TIFFReadDirEntryErr err;
    1583             :     uint32_t count;
    1584             :     void *origdata;
    1585             :     int8_t *data;
    1586           0 :     switch (direntry->tdir_type)
    1587             :     {
    1588           0 :         case TIFF_UNDEFINED:
    1589             :         case TIFF_BYTE:
    1590             :         case TIFF_SBYTE:
    1591             :         case TIFF_SHORT:
    1592             :         case TIFF_SSHORT:
    1593             :         case TIFF_LONG:
    1594             :         case TIFF_SLONG:
    1595             :         case TIFF_LONG8:
    1596             :         case TIFF_SLONG8:
    1597           0 :             break;
    1598           0 :         default:
    1599           0 :             return (TIFFReadDirEntryErrType);
    1600             :     }
    1601           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
    1602           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1603             :     {
    1604           0 :         *value = 0;
    1605           0 :         return (err);
    1606             :     }
    1607           0 :     switch (direntry->tdir_type)
    1608             :     {
    1609           0 :         case TIFF_UNDEFINED:
    1610             :         case TIFF_BYTE:
    1611             :         {
    1612             :             uint8_t *m;
    1613             :             uint32_t n;
    1614           0 :             m = (uint8_t *)origdata;
    1615           0 :             for (n = 0; n < count; n++)
    1616             :             {
    1617           0 :                 err = TIFFReadDirEntryCheckRangeSbyteByte(*m);
    1618           0 :                 if (err != TIFFReadDirEntryErrOk)
    1619             :                 {
    1620           0 :                     _TIFFfreeExt(tif, origdata);
    1621           0 :                     return (err);
    1622             :                 }
    1623           0 :                 m++;
    1624             :             }
    1625           0 :             *value = (int8_t *)origdata;
    1626           0 :             return (TIFFReadDirEntryErrOk);
    1627             :         }
    1628           0 :         case TIFF_SBYTE:
    1629           0 :             *value = (int8_t *)origdata;
    1630           0 :             return (TIFFReadDirEntryErrOk);
    1631           0 :         default:
    1632           0 :             break;
    1633             :     }
    1634           0 :     data = (int8_t *)_TIFFmallocExt(tif, count);
    1635           0 :     if (data == 0)
    1636             :     {
    1637           0 :         _TIFFfreeExt(tif, origdata);
    1638           0 :         return (TIFFReadDirEntryErrAlloc);
    1639             :     }
    1640           0 :     switch (direntry->tdir_type)
    1641             :     {
    1642           0 :         case TIFF_SHORT:
    1643             :         {
    1644             :             uint16_t *ma;
    1645             :             int8_t *mb;
    1646             :             uint32_t n;
    1647           0 :             ma = (uint16_t *)origdata;
    1648           0 :             mb = data;
    1649           0 :             for (n = 0; n < count; n++)
    1650             :             {
    1651           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1652           0 :                     TIFFSwabShort(ma);
    1653           0 :                 err = TIFFReadDirEntryCheckRangeSbyteShort(*ma);
    1654           0 :                 if (err != TIFFReadDirEntryErrOk)
    1655           0 :                     break;
    1656           0 :                 *mb++ = (int8_t)(*ma++);
    1657             :             }
    1658             :         }
    1659           0 :         break;
    1660           0 :         case TIFF_SSHORT:
    1661             :         {
    1662             :             int16_t *ma;
    1663             :             int8_t *mb;
    1664             :             uint32_t n;
    1665           0 :             ma = (int16_t *)origdata;
    1666           0 :             mb = data;
    1667           0 :             for (n = 0; n < count; n++)
    1668             :             {
    1669           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1670           0 :                     TIFFSwabShort((uint16_t *)ma);
    1671           0 :                 err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
    1672           0 :                 if (err != TIFFReadDirEntryErrOk)
    1673           0 :                     break;
    1674           0 :                 *mb++ = (int8_t)(*ma++);
    1675             :             }
    1676             :         }
    1677           0 :         break;
    1678           0 :         case TIFF_LONG:
    1679             :         {
    1680             :             uint32_t *ma;
    1681             :             int8_t *mb;
    1682             :             uint32_t n;
    1683           0 :             ma = (uint32_t *)origdata;
    1684           0 :             mb = data;
    1685           0 :             for (n = 0; n < count; n++)
    1686             :             {
    1687           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1688           0 :                     TIFFSwabLong(ma);
    1689           0 :                 err = TIFFReadDirEntryCheckRangeSbyteLong(*ma);
    1690           0 :                 if (err != TIFFReadDirEntryErrOk)
    1691           0 :                     break;
    1692           0 :                 *mb++ = (int8_t)(*ma++);
    1693             :             }
    1694             :         }
    1695           0 :         break;
    1696           0 :         case TIFF_SLONG:
    1697             :         {
    1698             :             int32_t *ma;
    1699             :             int8_t *mb;
    1700             :             uint32_t n;
    1701           0 :             ma = (int32_t *)origdata;
    1702           0 :             mb = data;
    1703           0 :             for (n = 0; n < count; n++)
    1704             :             {
    1705           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1706           0 :                     TIFFSwabLong((uint32_t *)ma);
    1707           0 :                 err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
    1708           0 :                 if (err != TIFFReadDirEntryErrOk)
    1709           0 :                     break;
    1710           0 :                 *mb++ = (int8_t)(*ma++);
    1711             :             }
    1712             :         }
    1713           0 :         break;
    1714           0 :         case TIFF_LONG8:
    1715             :         {
    1716             :             uint64_t *ma;
    1717             :             int8_t *mb;
    1718             :             uint32_t n;
    1719           0 :             ma = (uint64_t *)origdata;
    1720           0 :             mb = data;
    1721           0 :             for (n = 0; n < count; n++)
    1722             :             {
    1723           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1724           0 :                     TIFFSwabLong8(ma);
    1725           0 :                 err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
    1726           0 :                 if (err != TIFFReadDirEntryErrOk)
    1727           0 :                     break;
    1728           0 :                 *mb++ = (int8_t)(*ma++);
    1729             :             }
    1730             :         }
    1731           0 :         break;
    1732           0 :         case TIFF_SLONG8:
    1733             :         {
    1734             :             int64_t *ma;
    1735             :             int8_t *mb;
    1736             :             uint32_t n;
    1737           0 :             ma = (int64_t *)origdata;
    1738           0 :             mb = data;
    1739           0 :             for (n = 0; n < count; n++)
    1740             :             {
    1741           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1742           0 :                     TIFFSwabLong8((uint64_t *)ma);
    1743           0 :                 err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
    1744           0 :                 if (err != TIFFReadDirEntryErrOk)
    1745           0 :                     break;
    1746           0 :                 *mb++ = (int8_t)(*ma++);
    1747             :             }
    1748             :         }
    1749           0 :         break;
    1750           0 :         default:
    1751           0 :             break;
    1752             :     }
    1753           0 :     _TIFFfreeExt(tif, origdata);
    1754           0 :     if (err != TIFFReadDirEntryErrOk)
    1755             :     {
    1756           0 :         _TIFFfreeExt(tif, data);
    1757           0 :         return (err);
    1758             :     }
    1759           0 :     *value = data;
    1760           0 :     return (TIFFReadDirEntryErrOk);
    1761             : }
    1762             : 
    1763             : static enum TIFFReadDirEntryErr
    1764       68376 : TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
    1765             : {
    1766             :     enum TIFFReadDirEntryErr err;
    1767             :     uint32_t count;
    1768             :     void *origdata;
    1769             :     uint16_t *data;
    1770       68376 :     switch (direntry->tdir_type)
    1771             :     {
    1772       68356 :         case TIFF_BYTE:
    1773             :         case TIFF_SBYTE:
    1774             :         case TIFF_SHORT:
    1775             :         case TIFF_SSHORT:
    1776             :         case TIFF_LONG:
    1777             :         case TIFF_SLONG:
    1778             :         case TIFF_LONG8:
    1779             :         case TIFF_SLONG8:
    1780       68356 :             break;
    1781          20 :         default:
    1782          20 :             return (TIFFReadDirEntryErrType);
    1783             :     }
    1784       68356 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
    1785       68348 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1786             :     {
    1787          13 :         *value = 0;
    1788          13 :         return (err);
    1789             :     }
    1790       68335 :     switch (direntry->tdir_type)
    1791             :     {
    1792       68295 :         case TIFF_SHORT:
    1793       68295 :             *value = (uint16_t *)origdata;
    1794       68295 :             if (tif->tif_flags & TIFF_SWAB)
    1795        1014 :                 TIFFSwabArrayOfShort(*value, count);
    1796       68294 :             return (TIFFReadDirEntryErrOk);
    1797           0 :         case TIFF_SSHORT:
    1798             :         {
    1799             :             int16_t *m;
    1800             :             uint32_t n;
    1801           0 :             m = (int16_t *)origdata;
    1802           0 :             for (n = 0; n < count; n++)
    1803             :             {
    1804           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1805           0 :                     TIFFSwabShort((uint16_t *)m);
    1806           0 :                 err = TIFFReadDirEntryCheckRangeShortSshort(*m);
    1807           0 :                 if (err != TIFFReadDirEntryErrOk)
    1808             :                 {
    1809           0 :                     _TIFFfreeExt(tif, origdata);
    1810           0 :                     return (err);
    1811             :                 }
    1812           0 :                 m++;
    1813             :             }
    1814           0 :             *value = (uint16_t *)origdata;
    1815           0 :             return (TIFFReadDirEntryErrOk);
    1816             :         }
    1817          40 :         default:
    1818          40 :             break;
    1819             :     }
    1820          40 :     data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
    1821           0 :     if (data == 0)
    1822             :     {
    1823           0 :         _TIFFfreeExt(tif, origdata);
    1824           0 :         return (TIFFReadDirEntryErrAlloc);
    1825             :     }
    1826           0 :     switch (direntry->tdir_type)
    1827             :     {
    1828           0 :         case TIFF_BYTE:
    1829             :         {
    1830             :             uint8_t *ma;
    1831             :             uint16_t *mb;
    1832             :             uint32_t n;
    1833           0 :             ma = (uint8_t *)origdata;
    1834           0 :             mb = data;
    1835           0 :             for (n = 0; n < count; n++)
    1836           0 :                 *mb++ = (uint16_t)(*ma++);
    1837             :         }
    1838           0 :         break;
    1839           0 :         case TIFF_SBYTE:
    1840             :         {
    1841             :             int8_t *ma;
    1842             :             uint16_t *mb;
    1843             :             uint32_t n;
    1844           0 :             ma = (int8_t *)origdata;
    1845           0 :             mb = data;
    1846           0 :             for (n = 0; n < count; n++)
    1847             :             {
    1848           0 :                 err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
    1849           0 :                 if (err != TIFFReadDirEntryErrOk)
    1850           0 :                     break;
    1851           0 :                 *mb++ = (uint16_t)(*ma++);
    1852             :             }
    1853             :         }
    1854           0 :         break;
    1855           0 :         case TIFF_LONG:
    1856             :         {
    1857             :             uint32_t *ma;
    1858             :             uint16_t *mb;
    1859             :             uint32_t n;
    1860           0 :             ma = (uint32_t *)origdata;
    1861           0 :             mb = data;
    1862           0 :             for (n = 0; n < count; n++)
    1863             :             {
    1864           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1865           0 :                     TIFFSwabLong(ma);
    1866           0 :                 err = TIFFReadDirEntryCheckRangeShortLong(*ma);
    1867           0 :                 if (err != TIFFReadDirEntryErrOk)
    1868           0 :                     break;
    1869           0 :                 *mb++ = (uint16_t)(*ma++);
    1870             :             }
    1871             :         }
    1872           0 :         break;
    1873           0 :         case TIFF_SLONG:
    1874             :         {
    1875             :             int32_t *ma;
    1876             :             uint16_t *mb;
    1877             :             uint32_t n;
    1878           0 :             ma = (int32_t *)origdata;
    1879           0 :             mb = data;
    1880           0 :             for (n = 0; n < count; n++)
    1881             :             {
    1882           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1883           0 :                     TIFFSwabLong((uint32_t *)ma);
    1884           0 :                 err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
    1885           0 :                 if (err != TIFFReadDirEntryErrOk)
    1886           0 :                     break;
    1887           0 :                 *mb++ = (uint16_t)(*ma++);
    1888             :             }
    1889             :         }
    1890           0 :         break;
    1891           0 :         case TIFF_LONG8:
    1892             :         {
    1893             :             uint64_t *ma;
    1894             :             uint16_t *mb;
    1895             :             uint32_t n;
    1896           0 :             ma = (uint64_t *)origdata;
    1897           0 :             mb = data;
    1898           0 :             for (n = 0; n < count; n++)
    1899             :             {
    1900           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1901           0 :                     TIFFSwabLong8(ma);
    1902           0 :                 err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
    1903           0 :                 if (err != TIFFReadDirEntryErrOk)
    1904           0 :                     break;
    1905           0 :                 *mb++ = (uint16_t)(*ma++);
    1906             :             }
    1907             :         }
    1908           0 :         break;
    1909           0 :         case TIFF_SLONG8:
    1910             :         {
    1911             :             int64_t *ma;
    1912             :             uint16_t *mb;
    1913             :             uint32_t n;
    1914           0 :             ma = (int64_t *)origdata;
    1915           0 :             mb = data;
    1916           0 :             for (n = 0; n < count; n++)
    1917             :             {
    1918           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1919           0 :                     TIFFSwabLong8((uint64_t *)ma);
    1920           0 :                 err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
    1921           0 :                 if (err != TIFFReadDirEntryErrOk)
    1922           0 :                     break;
    1923           0 :                 *mb++ = (uint16_t)(*ma++);
    1924             :             }
    1925             :         }
    1926           0 :         break;
    1927           0 :         default:
    1928           0 :             break;
    1929             :     }
    1930           0 :     _TIFFfreeExt(tif, origdata);
    1931           0 :     if (err != TIFFReadDirEntryErrOk)
    1932             :     {
    1933           0 :         _TIFFfreeExt(tif, data);
    1934           0 :         return (err);
    1935             :     }
    1936           0 :     *value = data;
    1937           0 :     return (TIFFReadDirEntryErrOk);
    1938             : }
    1939             : 
    1940             : static enum TIFFReadDirEntryErr
    1941           0 : TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
    1942             : {
    1943             :     enum TIFFReadDirEntryErr err;
    1944             :     uint32_t count;
    1945             :     void *origdata;
    1946             :     int16_t *data;
    1947           0 :     switch (direntry->tdir_type)
    1948             :     {
    1949           0 :         case TIFF_BYTE:
    1950             :         case TIFF_SBYTE:
    1951             :         case TIFF_SHORT:
    1952             :         case TIFF_SSHORT:
    1953             :         case TIFF_LONG:
    1954             :         case TIFF_SLONG:
    1955             :         case TIFF_LONG8:
    1956             :         case TIFF_SLONG8:
    1957           0 :             break;
    1958           0 :         default:
    1959           0 :             return (TIFFReadDirEntryErrType);
    1960             :     }
    1961           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
    1962           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    1963             :     {
    1964           0 :         *value = 0;
    1965           0 :         return (err);
    1966             :     }
    1967           0 :     switch (direntry->tdir_type)
    1968             :     {
    1969           0 :         case TIFF_SHORT:
    1970             :         {
    1971             :             uint16_t *m;
    1972             :             uint32_t n;
    1973           0 :             m = (uint16_t *)origdata;
    1974           0 :             for (n = 0; n < count; n++)
    1975             :             {
    1976           0 :                 if (tif->tif_flags & TIFF_SWAB)
    1977           0 :                     TIFFSwabShort(m);
    1978           0 :                 err = TIFFReadDirEntryCheckRangeSshortShort(*m);
    1979           0 :                 if (err != TIFFReadDirEntryErrOk)
    1980             :                 {
    1981           0 :                     _TIFFfreeExt(tif, origdata);
    1982           0 :                     return (err);
    1983             :                 }
    1984           0 :                 m++;
    1985             :             }
    1986           0 :             *value = (int16_t *)origdata;
    1987           0 :             return (TIFFReadDirEntryErrOk);
    1988             :         }
    1989           0 :         case TIFF_SSHORT:
    1990           0 :             *value = (int16_t *)origdata;
    1991           0 :             if (tif->tif_flags & TIFF_SWAB)
    1992           0 :                 TIFFSwabArrayOfShort((uint16_t *)(*value), count);
    1993           0 :             return (TIFFReadDirEntryErrOk);
    1994           0 :         default:
    1995           0 :             break;
    1996             :     }
    1997           0 :     data = (int16_t *)_TIFFmallocExt(tif, count * 2);
    1998           0 :     if (data == 0)
    1999             :     {
    2000           0 :         _TIFFfreeExt(tif, origdata);
    2001           0 :         return (TIFFReadDirEntryErrAlloc);
    2002             :     }
    2003           0 :     switch (direntry->tdir_type)
    2004             :     {
    2005           0 :         case TIFF_BYTE:
    2006             :         {
    2007             :             uint8_t *ma;
    2008             :             int16_t *mb;
    2009             :             uint32_t n;
    2010           0 :             ma = (uint8_t *)origdata;
    2011           0 :             mb = data;
    2012           0 :             for (n = 0; n < count; n++)
    2013           0 :                 *mb++ = (int16_t)(*ma++);
    2014             :         }
    2015           0 :         break;
    2016           0 :         case TIFF_SBYTE:
    2017             :         {
    2018             :             int8_t *ma;
    2019             :             int16_t *mb;
    2020             :             uint32_t n;
    2021           0 :             ma = (int8_t *)origdata;
    2022           0 :             mb = data;
    2023           0 :             for (n = 0; n < count; n++)
    2024           0 :                 *mb++ = (int16_t)(*ma++);
    2025             :         }
    2026           0 :         break;
    2027           0 :         case TIFF_LONG:
    2028             :         {
    2029             :             uint32_t *ma;
    2030             :             int16_t *mb;
    2031             :             uint32_t n;
    2032           0 :             ma = (uint32_t *)origdata;
    2033           0 :             mb = data;
    2034           0 :             for (n = 0; n < count; n++)
    2035             :             {
    2036           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2037           0 :                     TIFFSwabLong(ma);
    2038           0 :                 err = TIFFReadDirEntryCheckRangeSshortLong(*ma);
    2039           0 :                 if (err != TIFFReadDirEntryErrOk)
    2040           0 :                     break;
    2041           0 :                 *mb++ = (int16_t)(*ma++);
    2042             :             }
    2043             :         }
    2044           0 :         break;
    2045           0 :         case TIFF_SLONG:
    2046             :         {
    2047             :             int32_t *ma;
    2048             :             int16_t *mb;
    2049             :             uint32_t n;
    2050           0 :             ma = (int32_t *)origdata;
    2051           0 :             mb = data;
    2052           0 :             for (n = 0; n < count; n++)
    2053             :             {
    2054           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2055           0 :                     TIFFSwabLong((uint32_t *)ma);
    2056           0 :                 err = TIFFReadDirEntryCheckRangeSshortSlong(*ma);
    2057           0 :                 if (err != TIFFReadDirEntryErrOk)
    2058           0 :                     break;
    2059           0 :                 *mb++ = (int16_t)(*ma++);
    2060             :             }
    2061             :         }
    2062           0 :         break;
    2063           0 :         case TIFF_LONG8:
    2064             :         {
    2065             :             uint64_t *ma;
    2066             :             int16_t *mb;
    2067             :             uint32_t n;
    2068           0 :             ma = (uint64_t *)origdata;
    2069           0 :             mb = data;
    2070           0 :             for (n = 0; n < count; n++)
    2071             :             {
    2072           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2073           0 :                     TIFFSwabLong8(ma);
    2074           0 :                 err = TIFFReadDirEntryCheckRangeSshortLong8(*ma);
    2075           0 :                 if (err != TIFFReadDirEntryErrOk)
    2076           0 :                     break;
    2077           0 :                 *mb++ = (int16_t)(*ma++);
    2078             :             }
    2079             :         }
    2080           0 :         break;
    2081           0 :         case TIFF_SLONG8:
    2082             :         {
    2083             :             int64_t *ma;
    2084             :             int16_t *mb;
    2085             :             uint32_t n;
    2086           0 :             ma = (int64_t *)origdata;
    2087           0 :             mb = data;
    2088           0 :             for (n = 0; n < count; n++)
    2089             :             {
    2090           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2091           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2092           0 :                 err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
    2093           0 :                 if (err != TIFFReadDirEntryErrOk)
    2094           0 :                     break;
    2095           0 :                 *mb++ = (int16_t)(*ma++);
    2096             :             }
    2097             :         }
    2098           0 :         break;
    2099           0 :         default:
    2100           0 :             break;
    2101             :     }
    2102           0 :     _TIFFfreeExt(tif, origdata);
    2103           0 :     if (err != TIFFReadDirEntryErrOk)
    2104             :     {
    2105           0 :         _TIFFfreeExt(tif, data);
    2106           0 :         return (err);
    2107             :     }
    2108           0 :     *value = data;
    2109           0 :     return (TIFFReadDirEntryErrOk);
    2110             : }
    2111             : 
    2112             : static enum TIFFReadDirEntryErr
    2113         801 : TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
    2114             : {
    2115             :     enum TIFFReadDirEntryErr err;
    2116             :     uint32_t count;
    2117             :     void *origdata;
    2118             :     uint32_t *data;
    2119         801 :     switch (direntry->tdir_type)
    2120             :     {
    2121         801 :         case TIFF_BYTE:
    2122             :         case TIFF_SBYTE:
    2123             :         case TIFF_SHORT:
    2124             :         case TIFF_SSHORT:
    2125             :         case TIFF_LONG:
    2126             :         case TIFF_SLONG:
    2127             :         case TIFF_LONG8:
    2128             :         case TIFF_SLONG8:
    2129             :         case TIFF_IFD:
    2130             :         case TIFF_IFD8:
    2131         801 :             break;
    2132           0 :         default:
    2133           0 :             return (TIFFReadDirEntryErrType);
    2134             :     }
    2135         801 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
    2136         801 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2137             :     {
    2138           9 :         *value = 0;
    2139           9 :         return (err);
    2140             :     }
    2141         792 :     switch (direntry->tdir_type)
    2142             :     {
    2143         792 :         case TIFF_LONG:
    2144             :         case TIFF_IFD:
    2145         792 :             *value = (uint32_t *)origdata;
    2146         792 :             if (tif->tif_flags & TIFF_SWAB)
    2147           0 :                 TIFFSwabArrayOfLong(*value, count);
    2148         792 :             return (TIFFReadDirEntryErrOk);
    2149           0 :         case TIFF_SLONG:
    2150             :         {
    2151             :             int32_t *m;
    2152             :             uint32_t n;
    2153           0 :             m = (int32_t *)origdata;
    2154           0 :             for (n = 0; n < count; n++)
    2155             :             {
    2156           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2157           0 :                     TIFFSwabLong((uint32_t *)m);
    2158           0 :                 err = TIFFReadDirEntryCheckRangeLongSlong(*m);
    2159           0 :                 if (err != TIFFReadDirEntryErrOk)
    2160             :                 {
    2161           0 :                     _TIFFfreeExt(tif, origdata);
    2162           0 :                     return (err);
    2163             :                 }
    2164           0 :                 m++;
    2165             :             }
    2166           0 :             *value = (uint32_t *)origdata;
    2167           0 :             return (TIFFReadDirEntryErrOk);
    2168             :         }
    2169           0 :         default:
    2170           0 :             break;
    2171             :     }
    2172           0 :     data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
    2173           0 :     if (data == 0)
    2174             :     {
    2175           0 :         _TIFFfreeExt(tif, origdata);
    2176           0 :         return (TIFFReadDirEntryErrAlloc);
    2177             :     }
    2178           0 :     switch (direntry->tdir_type)
    2179             :     {
    2180           0 :         case TIFF_BYTE:
    2181             :         {
    2182             :             uint8_t *ma;
    2183             :             uint32_t *mb;
    2184             :             uint32_t n;
    2185           0 :             ma = (uint8_t *)origdata;
    2186           0 :             mb = data;
    2187           0 :             for (n = 0; n < count; n++)
    2188           0 :                 *mb++ = (uint32_t)(*ma++);
    2189             :         }
    2190           0 :         break;
    2191           0 :         case TIFF_SBYTE:
    2192             :         {
    2193             :             int8_t *ma;
    2194             :             uint32_t *mb;
    2195             :             uint32_t n;
    2196           0 :             ma = (int8_t *)origdata;
    2197           0 :             mb = data;
    2198           0 :             for (n = 0; n < count; n++)
    2199             :             {
    2200           0 :                 err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
    2201           0 :                 if (err != TIFFReadDirEntryErrOk)
    2202           0 :                     break;
    2203           0 :                 *mb++ = (uint32_t)(*ma++);
    2204             :             }
    2205             :         }
    2206           0 :         break;
    2207           0 :         case TIFF_SHORT:
    2208             :         {
    2209             :             uint16_t *ma;
    2210             :             uint32_t *mb;
    2211             :             uint32_t n;
    2212           0 :             ma = (uint16_t *)origdata;
    2213           0 :             mb = data;
    2214           0 :             for (n = 0; n < count; n++)
    2215             :             {
    2216           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2217           0 :                     TIFFSwabShort(ma);
    2218           0 :                 *mb++ = (uint32_t)(*ma++);
    2219             :             }
    2220             :         }
    2221           0 :         break;
    2222           0 :         case TIFF_SSHORT:
    2223             :         {
    2224             :             int16_t *ma;
    2225             :             uint32_t *mb;
    2226             :             uint32_t n;
    2227           0 :             ma = (int16_t *)origdata;
    2228           0 :             mb = data;
    2229           0 :             for (n = 0; n < count; n++)
    2230             :             {
    2231           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2232           0 :                     TIFFSwabShort((uint16_t *)ma);
    2233           0 :                 err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
    2234           0 :                 if (err != TIFFReadDirEntryErrOk)
    2235           0 :                     break;
    2236           0 :                 *mb++ = (uint32_t)(*ma++);
    2237             :             }
    2238             :         }
    2239           0 :         break;
    2240           0 :         case TIFF_LONG8:
    2241             :         case TIFF_IFD8:
    2242             :         {
    2243             :             uint64_t *ma;
    2244             :             uint32_t *mb;
    2245             :             uint32_t n;
    2246           0 :             ma = (uint64_t *)origdata;
    2247           0 :             mb = data;
    2248           0 :             for (n = 0; n < count; n++)
    2249             :             {
    2250           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2251           0 :                     TIFFSwabLong8(ma);
    2252           0 :                 err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
    2253           0 :                 if (err != TIFFReadDirEntryErrOk)
    2254           0 :                     break;
    2255           0 :                 *mb++ = (uint32_t)(*ma++);
    2256             :             }
    2257             :         }
    2258           0 :         break;
    2259           0 :         case TIFF_SLONG8:
    2260             :         {
    2261             :             int64_t *ma;
    2262             :             uint32_t *mb;
    2263             :             uint32_t n;
    2264           0 :             ma = (int64_t *)origdata;
    2265           0 :             mb = data;
    2266           0 :             for (n = 0; n < count; n++)
    2267             :             {
    2268           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2269           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2270           0 :                 err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
    2271           0 :                 if (err != TIFFReadDirEntryErrOk)
    2272           0 :                     break;
    2273           0 :                 *mb++ = (uint32_t)(*ma++);
    2274             :             }
    2275             :         }
    2276           0 :         break;
    2277           0 :         default:
    2278           0 :             break;
    2279             :     }
    2280           0 :     _TIFFfreeExt(tif, origdata);
    2281           0 :     if (err != TIFFReadDirEntryErrOk)
    2282             :     {
    2283           0 :         _TIFFfreeExt(tif, data);
    2284           0 :         return (err);
    2285             :     }
    2286           0 :     *value = data;
    2287           0 :     return (TIFFReadDirEntryErrOk);
    2288             : }
    2289             : 
    2290             : static enum TIFFReadDirEntryErr
    2291           0 : TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
    2292             : {
    2293             :     enum TIFFReadDirEntryErr err;
    2294             :     uint32_t count;
    2295             :     void *origdata;
    2296             :     int32_t *data;
    2297           0 :     switch (direntry->tdir_type)
    2298             :     {
    2299           0 :         case TIFF_BYTE:
    2300             :         case TIFF_SBYTE:
    2301             :         case TIFF_SHORT:
    2302             :         case TIFF_SSHORT:
    2303             :         case TIFF_LONG:
    2304             :         case TIFF_SLONG:
    2305             :         case TIFF_LONG8:
    2306             :         case TIFF_SLONG8:
    2307           0 :             break;
    2308           0 :         default:
    2309           0 :             return (TIFFReadDirEntryErrType);
    2310             :     }
    2311           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
    2312           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2313             :     {
    2314           0 :         *value = 0;
    2315           0 :         return (err);
    2316             :     }
    2317           0 :     switch (direntry->tdir_type)
    2318             :     {
    2319           0 :         case TIFF_LONG:
    2320             :         {
    2321             :             uint32_t *m;
    2322             :             uint32_t n;
    2323           0 :             m = (uint32_t *)origdata;
    2324           0 :             for (n = 0; n < count; n++)
    2325             :             {
    2326           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2327           0 :                     TIFFSwabLong((uint32_t *)m);
    2328           0 :                 err = TIFFReadDirEntryCheckRangeSlongLong(*m);
    2329           0 :                 if (err != TIFFReadDirEntryErrOk)
    2330             :                 {
    2331           0 :                     _TIFFfreeExt(tif, origdata);
    2332           0 :                     return (err);
    2333             :                 }
    2334           0 :                 m++;
    2335             :             }
    2336           0 :             *value = (int32_t *)origdata;
    2337           0 :             return (TIFFReadDirEntryErrOk);
    2338             :         }
    2339           0 :         case TIFF_SLONG:
    2340           0 :             *value = (int32_t *)origdata;
    2341           0 :             if (tif->tif_flags & TIFF_SWAB)
    2342           0 :                 TIFFSwabArrayOfLong((uint32_t *)(*value), count);
    2343           0 :             return (TIFFReadDirEntryErrOk);
    2344           0 :         default:
    2345           0 :             break;
    2346             :     }
    2347           0 :     data = (int32_t *)_TIFFmallocExt(tif, count * 4);
    2348           0 :     if (data == 0)
    2349             :     {
    2350           0 :         _TIFFfreeExt(tif, origdata);
    2351           0 :         return (TIFFReadDirEntryErrAlloc);
    2352             :     }
    2353           0 :     switch (direntry->tdir_type)
    2354             :     {
    2355           0 :         case TIFF_BYTE:
    2356             :         {
    2357             :             uint8_t *ma;
    2358             :             int32_t *mb;
    2359             :             uint32_t n;
    2360           0 :             ma = (uint8_t *)origdata;
    2361           0 :             mb = data;
    2362           0 :             for (n = 0; n < count; n++)
    2363           0 :                 *mb++ = (int32_t)(*ma++);
    2364             :         }
    2365           0 :         break;
    2366           0 :         case TIFF_SBYTE:
    2367             :         {
    2368             :             int8_t *ma;
    2369             :             int32_t *mb;
    2370             :             uint32_t n;
    2371           0 :             ma = (int8_t *)origdata;
    2372           0 :             mb = data;
    2373           0 :             for (n = 0; n < count; n++)
    2374           0 :                 *mb++ = (int32_t)(*ma++);
    2375             :         }
    2376           0 :         break;
    2377           0 :         case TIFF_SHORT:
    2378             :         {
    2379             :             uint16_t *ma;
    2380             :             int32_t *mb;
    2381             :             uint32_t n;
    2382           0 :             ma = (uint16_t *)origdata;
    2383           0 :             mb = data;
    2384           0 :             for (n = 0; n < count; n++)
    2385             :             {
    2386           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2387           0 :                     TIFFSwabShort(ma);
    2388           0 :                 *mb++ = (int32_t)(*ma++);
    2389             :             }
    2390             :         }
    2391           0 :         break;
    2392           0 :         case TIFF_SSHORT:
    2393             :         {
    2394             :             int16_t *ma;
    2395             :             int32_t *mb;
    2396             :             uint32_t n;
    2397           0 :             ma = (int16_t *)origdata;
    2398           0 :             mb = data;
    2399           0 :             for (n = 0; n < count; n++)
    2400             :             {
    2401           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2402           0 :                     TIFFSwabShort((uint16_t *)ma);
    2403           0 :                 *mb++ = (int32_t)(*ma++);
    2404             :             }
    2405             :         }
    2406           0 :         break;
    2407           0 :         case TIFF_LONG8:
    2408             :         {
    2409             :             uint64_t *ma;
    2410             :             int32_t *mb;
    2411             :             uint32_t n;
    2412           0 :             ma = (uint64_t *)origdata;
    2413           0 :             mb = data;
    2414           0 :             for (n = 0; n < count; n++)
    2415             :             {
    2416           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2417           0 :                     TIFFSwabLong8(ma);
    2418           0 :                 err = TIFFReadDirEntryCheckRangeSlongLong8(*ma);
    2419           0 :                 if (err != TIFFReadDirEntryErrOk)
    2420           0 :                     break;
    2421           0 :                 *mb++ = (int32_t)(*ma++);
    2422             :             }
    2423             :         }
    2424           0 :         break;
    2425           0 :         case TIFF_SLONG8:
    2426             :         {
    2427             :             int64_t *ma;
    2428             :             int32_t *mb;
    2429             :             uint32_t n;
    2430           0 :             ma = (int64_t *)origdata;
    2431           0 :             mb = data;
    2432           0 :             for (n = 0; n < count; n++)
    2433             :             {
    2434           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2435           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2436           0 :                 err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
    2437           0 :                 if (err != TIFFReadDirEntryErrOk)
    2438           0 :                     break;
    2439           0 :                 *mb++ = (int32_t)(*ma++);
    2440             :             }
    2441             :         }
    2442           0 :         break;
    2443           0 :         default:
    2444           0 :             break;
    2445             :     }
    2446           0 :     _TIFFfreeExt(tif, origdata);
    2447           0 :     if (err != TIFFReadDirEntryErrOk)
    2448             :     {
    2449           0 :         _TIFFfreeExt(tif, data);
    2450           0 :         return (err);
    2451             :     }
    2452           0 :     *value = data;
    2453           0 :     return (TIFFReadDirEntryErrOk);
    2454             : }
    2455             : 
    2456             : static enum TIFFReadDirEntryErr
    2457       84701 : TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
    2458             :                                     uint64_t **value, uint64_t maxcount)
    2459             : {
    2460             :     enum TIFFReadDirEntryErr err;
    2461             :     uint32_t count;
    2462             :     void *origdata;
    2463             :     uint64_t *data;
    2464       84701 :     switch (direntry->tdir_type)
    2465             :     {
    2466       84685 :         case TIFF_BYTE:
    2467             :         case TIFF_SBYTE:
    2468             :         case TIFF_SHORT:
    2469             :         case TIFF_SSHORT:
    2470             :         case TIFF_LONG:
    2471             :         case TIFF_SLONG:
    2472             :         case TIFF_LONG8:
    2473             :         case TIFF_SLONG8:
    2474             :         case TIFF_IFD:
    2475             :         case TIFF_IFD8:
    2476       84685 :             break;
    2477          16 :         default:
    2478          16 :             return (TIFFReadDirEntryErrType);
    2479             :     }
    2480       84685 :     err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
    2481             :                                          maxcount);
    2482       84705 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2483             :     {
    2484           7 :         *value = 0;
    2485           7 :         return (err);
    2486             :     }
    2487       84698 :     switch (direntry->tdir_type)
    2488             :     {
    2489         527 :         case TIFF_LONG8:
    2490             :         case TIFF_IFD8:
    2491         527 :             *value = (uint64_t *)origdata;
    2492         527 :             if (tif->tif_flags & TIFF_SWAB)
    2493          15 :                 TIFFSwabArrayOfLong8(*value, count);
    2494         527 :             return (TIFFReadDirEntryErrOk);
    2495           1 :         case TIFF_SLONG8:
    2496             :         {
    2497             :             int64_t *m;
    2498             :             uint32_t n;
    2499           1 :             m = (int64_t *)origdata;
    2500           2 :             for (n = 0; n < count; n++)
    2501             :             {
    2502           1 :                 if (tif->tif_flags & TIFF_SWAB)
    2503           0 :                     TIFFSwabLong8((uint64_t *)m);
    2504           1 :                 err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
    2505           1 :                 if (err != TIFFReadDirEntryErrOk)
    2506             :                 {
    2507           0 :                     _TIFFfreeExt(tif, origdata);
    2508           0 :                     return (err);
    2509             :                 }
    2510           1 :                 m++;
    2511             :             }
    2512           1 :             *value = (uint64_t *)origdata;
    2513           1 :             return (TIFFReadDirEntryErrOk);
    2514             :         }
    2515       84170 :         default:
    2516       84170 :             break;
    2517             :     }
    2518       84170 :     data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
    2519       84237 :     if (data == 0)
    2520             :     {
    2521          25 :         _TIFFfreeExt(tif, origdata);
    2522           0 :         return (TIFFReadDirEntryErrAlloc);
    2523             :     }
    2524       84212 :     switch (direntry->tdir_type)
    2525             :     {
    2526           1 :         case TIFF_BYTE:
    2527             :         {
    2528             :             uint8_t *ma;
    2529             :             uint64_t *mb;
    2530             :             uint32_t n;
    2531           1 :             ma = (uint8_t *)origdata;
    2532           1 :             mb = data;
    2533           2 :             for (n = 0; n < count; n++)
    2534           1 :                 *mb++ = (uint64_t)(*ma++);
    2535             :         }
    2536           1 :         break;
    2537           0 :         case TIFF_SBYTE:
    2538             :         {
    2539             :             int8_t *ma;
    2540             :             uint64_t *mb;
    2541             :             uint32_t n;
    2542           0 :             ma = (int8_t *)origdata;
    2543           0 :             mb = data;
    2544           0 :             for (n = 0; n < count; n++)
    2545             :             {
    2546           0 :                 err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
    2547           0 :                 if (err != TIFFReadDirEntryErrOk)
    2548           0 :                     break;
    2549           0 :                 *mb++ = (uint64_t)(*ma++);
    2550             :             }
    2551             :         }
    2552           0 :         break;
    2553        4113 :         case TIFF_SHORT:
    2554             :         {
    2555             :             uint16_t *ma;
    2556             :             uint64_t *mb;
    2557             :             uint32_t n;
    2558        4113 :             ma = (uint16_t *)origdata;
    2559        4113 :             mb = data;
    2560     3429410 :             for (n = 0; n < count; n++)
    2561             :             {
    2562     3425290 :                 if (tif->tif_flags & TIFF_SWAB)
    2563       13767 :                     TIFFSwabShort(ma);
    2564     3425290 :                 *mb++ = (uint64_t)(*ma++);
    2565             :             }
    2566             :         }
    2567        4113 :         break;
    2568           0 :         case TIFF_SSHORT:
    2569             :         {
    2570             :             int16_t *ma;
    2571             :             uint64_t *mb;
    2572             :             uint32_t n;
    2573           0 :             ma = (int16_t *)origdata;
    2574           0 :             mb = data;
    2575           0 :             for (n = 0; n < count; n++)
    2576             :             {
    2577           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2578           0 :                     TIFFSwabShort((uint16_t *)ma);
    2579           0 :                 err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
    2580           0 :                 if (err != TIFFReadDirEntryErrOk)
    2581           0 :                     break;
    2582           0 :                 *mb++ = (uint64_t)(*ma++);
    2583             :             }
    2584             :         }
    2585           0 :         break;
    2586       80098 :         case TIFF_LONG:
    2587             :         case TIFF_IFD:
    2588             :         {
    2589             :             uint32_t *ma;
    2590             :             uint64_t *mb;
    2591             :             uint32_t n;
    2592       80098 :             ma = (uint32_t *)origdata;
    2593       80098 :             mb = data;
    2594     7849830 :             for (n = 0; n < count; n++)
    2595             :             {
    2596     7769760 :                 if (tif->tif_flags & TIFF_SWAB)
    2597      415494 :                     TIFFSwabLong(ma);
    2598     7769740 :                 *mb++ = (uint64_t)(*ma++);
    2599             :             }
    2600             :         }
    2601       80069 :         break;
    2602           0 :         case TIFF_SLONG:
    2603             :         {
    2604             :             int32_t *ma;
    2605             :             uint64_t *mb;
    2606             :             uint32_t n;
    2607           0 :             ma = (int32_t *)origdata;
    2608           0 :             mb = data;
    2609           0 :             for (n = 0; n < count; n++)
    2610             :             {
    2611           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2612           0 :                     TIFFSwabLong((uint32_t *)ma);
    2613           0 :                 err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
    2614           0 :                 if (err != TIFFReadDirEntryErrOk)
    2615           0 :                     break;
    2616           0 :                 *mb++ = (uint64_t)(*ma++);
    2617             :             }
    2618             :         }
    2619           0 :         break;
    2620           0 :         default:
    2621           0 :             break;
    2622             :     }
    2623       84183 :     _TIFFfreeExt(tif, origdata);
    2624       84198 :     if (err != TIFFReadDirEntryErrOk)
    2625             :     {
    2626          39 :         _TIFFfreeExt(tif, data);
    2627           0 :         return (err);
    2628             :     }
    2629       84159 :     *value = data;
    2630       84159 :     return (TIFFReadDirEntryErrOk);
    2631             : }
    2632             : 
    2633             : static enum TIFFReadDirEntryErr
    2634           6 : TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
    2635             : {
    2636           6 :     return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
    2637             :                                                ~((uint64_t)0));
    2638             : }
    2639             : 
    2640             : static enum TIFFReadDirEntryErr
    2641           0 : TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
    2642             : {
    2643             :     enum TIFFReadDirEntryErr err;
    2644             :     uint32_t count;
    2645             :     void *origdata;
    2646             :     int64_t *data;
    2647           0 :     switch (direntry->tdir_type)
    2648             :     {
    2649           0 :         case TIFF_BYTE:
    2650             :         case TIFF_SBYTE:
    2651             :         case TIFF_SHORT:
    2652             :         case TIFF_SSHORT:
    2653             :         case TIFF_LONG:
    2654             :         case TIFF_SLONG:
    2655             :         case TIFF_LONG8:
    2656             :         case TIFF_SLONG8:
    2657           0 :             break;
    2658           0 :         default:
    2659           0 :             return (TIFFReadDirEntryErrType);
    2660             :     }
    2661           0 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
    2662           0 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2663             :     {
    2664           0 :         *value = 0;
    2665           0 :         return (err);
    2666             :     }
    2667           0 :     switch (direntry->tdir_type)
    2668             :     {
    2669           0 :         case TIFF_LONG8:
    2670             :         {
    2671             :             uint64_t *m;
    2672             :             uint32_t n;
    2673           0 :             m = (uint64_t *)origdata;
    2674           0 :             for (n = 0; n < count; n++)
    2675             :             {
    2676           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2677           0 :                     TIFFSwabLong8(m);
    2678           0 :                 err = TIFFReadDirEntryCheckRangeSlong8Long8(*m);
    2679           0 :                 if (err != TIFFReadDirEntryErrOk)
    2680             :                 {
    2681           0 :                     _TIFFfreeExt(tif, origdata);
    2682           0 :                     return (err);
    2683             :                 }
    2684           0 :                 m++;
    2685             :             }
    2686           0 :             *value = (int64_t *)origdata;
    2687           0 :             return (TIFFReadDirEntryErrOk);
    2688             :         }
    2689           0 :         case TIFF_SLONG8:
    2690           0 :             *value = (int64_t *)origdata;
    2691           0 :             if (tif->tif_flags & TIFF_SWAB)
    2692           0 :                 TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
    2693           0 :             return (TIFFReadDirEntryErrOk);
    2694           0 :         default:
    2695           0 :             break;
    2696             :     }
    2697           0 :     data = (int64_t *)_TIFFmallocExt(tif, count * 8);
    2698           0 :     if (data == 0)
    2699             :     {
    2700           0 :         _TIFFfreeExt(tif, origdata);
    2701           0 :         return (TIFFReadDirEntryErrAlloc);
    2702             :     }
    2703           0 :     switch (direntry->tdir_type)
    2704             :     {
    2705           0 :         case TIFF_BYTE:
    2706             :         {
    2707             :             uint8_t *ma;
    2708             :             int64_t *mb;
    2709             :             uint32_t n;
    2710           0 :             ma = (uint8_t *)origdata;
    2711           0 :             mb = data;
    2712           0 :             for (n = 0; n < count; n++)
    2713           0 :                 *mb++ = (int64_t)(*ma++);
    2714             :         }
    2715           0 :         break;
    2716           0 :         case TIFF_SBYTE:
    2717             :         {
    2718             :             int8_t *ma;
    2719             :             int64_t *mb;
    2720             :             uint32_t n;
    2721           0 :             ma = (int8_t *)origdata;
    2722           0 :             mb = data;
    2723           0 :             for (n = 0; n < count; n++)
    2724           0 :                 *mb++ = (int64_t)(*ma++);
    2725             :         }
    2726           0 :         break;
    2727           0 :         case TIFF_SHORT:
    2728             :         {
    2729             :             uint16_t *ma;
    2730             :             int64_t *mb;
    2731             :             uint32_t n;
    2732           0 :             ma = (uint16_t *)origdata;
    2733           0 :             mb = data;
    2734           0 :             for (n = 0; n < count; n++)
    2735             :             {
    2736           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2737           0 :                     TIFFSwabShort(ma);
    2738           0 :                 *mb++ = (int64_t)(*ma++);
    2739             :             }
    2740             :         }
    2741           0 :         break;
    2742           0 :         case TIFF_SSHORT:
    2743             :         {
    2744             :             int16_t *ma;
    2745             :             int64_t *mb;
    2746             :             uint32_t n;
    2747           0 :             ma = (int16_t *)origdata;
    2748           0 :             mb = data;
    2749           0 :             for (n = 0; n < count; n++)
    2750             :             {
    2751           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2752           0 :                     TIFFSwabShort((uint16_t *)ma);
    2753           0 :                 *mb++ = (int64_t)(*ma++);
    2754             :             }
    2755             :         }
    2756           0 :         break;
    2757           0 :         case TIFF_LONG:
    2758             :         {
    2759             :             uint32_t *ma;
    2760             :             int64_t *mb;
    2761             :             uint32_t n;
    2762           0 :             ma = (uint32_t *)origdata;
    2763           0 :             mb = data;
    2764           0 :             for (n = 0; n < count; n++)
    2765             :             {
    2766           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2767           0 :                     TIFFSwabLong(ma);
    2768           0 :                 *mb++ = (int64_t)(*ma++);
    2769             :             }
    2770             :         }
    2771           0 :         break;
    2772           0 :         case TIFF_SLONG:
    2773             :         {
    2774             :             int32_t *ma;
    2775             :             int64_t *mb;
    2776             :             uint32_t n;
    2777           0 :             ma = (int32_t *)origdata;
    2778           0 :             mb = data;
    2779           0 :             for (n = 0; n < count; n++)
    2780             :             {
    2781           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2782           0 :                     TIFFSwabLong((uint32_t *)ma);
    2783           0 :                 *mb++ = (int64_t)(*ma++);
    2784             :             }
    2785             :         }
    2786           0 :         break;
    2787           0 :         default:
    2788           0 :             break;
    2789             :     }
    2790           0 :     _TIFFfreeExt(tif, origdata);
    2791           0 :     *value = data;
    2792           0 :     return (TIFFReadDirEntryErrOk);
    2793             : }
    2794             : 
    2795             : static enum TIFFReadDirEntryErr
    2796        1027 : TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
    2797             : {
    2798             :     enum TIFFReadDirEntryErr err;
    2799             :     uint32_t count;
    2800             :     void *origdata;
    2801             :     float *data;
    2802        1027 :     switch (direntry->tdir_type)
    2803             :     {
    2804        1027 :         case TIFF_BYTE:
    2805             :         case TIFF_SBYTE:
    2806             :         case TIFF_SHORT:
    2807             :         case TIFF_SSHORT:
    2808             :         case TIFF_LONG:
    2809             :         case TIFF_SLONG:
    2810             :         case TIFF_LONG8:
    2811             :         case TIFF_SLONG8:
    2812             :         case TIFF_RATIONAL:
    2813             :         case TIFF_SRATIONAL:
    2814             :         case TIFF_FLOAT:
    2815             :         case TIFF_DOUBLE:
    2816        1027 :             break;
    2817           0 :         default:
    2818           0 :             return (TIFFReadDirEntryErrType);
    2819             :     }
    2820        1027 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
    2821        1027 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    2822             :     {
    2823           0 :         *value = 0;
    2824           0 :         return (err);
    2825             :     }
    2826        1027 :     switch (direntry->tdir_type)
    2827             :     {
    2828           0 :         case TIFF_FLOAT:
    2829           0 :             if (tif->tif_flags & TIFF_SWAB)
    2830           0 :                 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
    2831             :             TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
    2832           0 :             *value = (float *)origdata;
    2833           0 :             return (TIFFReadDirEntryErrOk);
    2834        1027 :         default:
    2835        1027 :             break;
    2836             :     }
    2837        1027 :     data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
    2838        1027 :     if (data == 0)
    2839             :     {
    2840           0 :         _TIFFfreeExt(tif, origdata);
    2841           0 :         return (TIFFReadDirEntryErrAlloc);
    2842             :     }
    2843        1027 :     switch (direntry->tdir_type)
    2844             :     {
    2845           0 :         case TIFF_BYTE:
    2846             :         {
    2847             :             uint8_t *ma;
    2848             :             float *mb;
    2849             :             uint32_t n;
    2850           0 :             ma = (uint8_t *)origdata;
    2851           0 :             mb = data;
    2852           0 :             for (n = 0; n < count; n++)
    2853           0 :                 *mb++ = (float)(*ma++);
    2854             :         }
    2855           0 :         break;
    2856           0 :         case TIFF_SBYTE:
    2857             :         {
    2858             :             int8_t *ma;
    2859             :             float *mb;
    2860             :             uint32_t n;
    2861           0 :             ma = (int8_t *)origdata;
    2862           0 :             mb = data;
    2863           0 :             for (n = 0; n < count; n++)
    2864           0 :                 *mb++ = (float)(*ma++);
    2865             :         }
    2866           0 :         break;
    2867           0 :         case TIFF_SHORT:
    2868             :         {
    2869             :             uint16_t *ma;
    2870             :             float *mb;
    2871             :             uint32_t n;
    2872           0 :             ma = (uint16_t *)origdata;
    2873           0 :             mb = data;
    2874           0 :             for (n = 0; n < count; n++)
    2875             :             {
    2876           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2877           0 :                     TIFFSwabShort(ma);
    2878           0 :                 *mb++ = (float)(*ma++);
    2879             :             }
    2880             :         }
    2881           0 :         break;
    2882           0 :         case TIFF_SSHORT:
    2883             :         {
    2884             :             int16_t *ma;
    2885             :             float *mb;
    2886             :             uint32_t n;
    2887           0 :             ma = (int16_t *)origdata;
    2888           0 :             mb = data;
    2889           0 :             for (n = 0; n < count; n++)
    2890             :             {
    2891           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2892           0 :                     TIFFSwabShort((uint16_t *)ma);
    2893           0 :                 *mb++ = (float)(*ma++);
    2894             :             }
    2895             :         }
    2896           0 :         break;
    2897           2 :         case TIFF_LONG:
    2898             :         {
    2899             :             uint32_t *ma;
    2900             :             float *mb;
    2901             :             uint32_t n;
    2902           2 :             ma = (uint32_t *)origdata;
    2903           2 :             mb = data;
    2904          14 :             for (n = 0; n < count; n++)
    2905             :             {
    2906          12 :                 if (tif->tif_flags & TIFF_SWAB)
    2907          12 :                     TIFFSwabLong(ma);
    2908          12 :                 *mb++ = (float)(*ma++);
    2909             :             }
    2910             :         }
    2911           2 :         break;
    2912           0 :         case TIFF_SLONG:
    2913             :         {
    2914             :             int32_t *ma;
    2915             :             float *mb;
    2916             :             uint32_t n;
    2917           0 :             ma = (int32_t *)origdata;
    2918           0 :             mb = data;
    2919           0 :             for (n = 0; n < count; n++)
    2920             :             {
    2921           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2922           0 :                     TIFFSwabLong((uint32_t *)ma);
    2923           0 :                 *mb++ = (float)(*ma++);
    2924             :             }
    2925             :         }
    2926           0 :         break;
    2927           0 :         case TIFF_LONG8:
    2928             :         {
    2929             :             uint64_t *ma;
    2930             :             float *mb;
    2931             :             uint32_t n;
    2932           0 :             ma = (uint64_t *)origdata;
    2933           0 :             mb = data;
    2934           0 :             for (n = 0; n < count; n++)
    2935             :             {
    2936           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2937           0 :                     TIFFSwabLong8(ma);
    2938           0 :                 *mb++ = (float)(*ma++);
    2939             :             }
    2940             :         }
    2941           0 :         break;
    2942           0 :         case TIFF_SLONG8:
    2943             :         {
    2944             :             int64_t *ma;
    2945             :             float *mb;
    2946             :             uint32_t n;
    2947           0 :             ma = (int64_t *)origdata;
    2948           0 :             mb = data;
    2949           0 :             for (n = 0; n < count; n++)
    2950             :             {
    2951           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2952           0 :                     TIFFSwabLong8((uint64_t *)ma);
    2953           0 :                 *mb++ = (float)(*ma++);
    2954             :             }
    2955             :         }
    2956           0 :         break;
    2957        1025 :         case TIFF_RATIONAL:
    2958             :         {
    2959             :             uint32_t *ma;
    2960             :             uint32_t maa;
    2961             :             uint32_t mab;
    2962             :             float *mb;
    2963             :             uint32_t n;
    2964        1025 :             ma = (uint32_t *)origdata;
    2965        1025 :             mb = data;
    2966        7056 :             for (n = 0; n < count; n++)
    2967             :             {
    2968        6031 :                 if (tif->tif_flags & TIFF_SWAB)
    2969          96 :                     TIFFSwabLong(ma);
    2970        6031 :                 maa = *ma++;
    2971        6031 :                 if (tif->tif_flags & TIFF_SWAB)
    2972          96 :                     TIFFSwabLong(ma);
    2973        6031 :                 mab = *ma++;
    2974        6031 :                 if (mab == 0)
    2975           0 :                     *mb++ = 0.0;
    2976             :                 else
    2977        6031 :                     *mb++ = (float)maa / (float)mab;
    2978             :             }
    2979             :         }
    2980        1025 :         break;
    2981           0 :         case TIFF_SRATIONAL:
    2982             :         {
    2983             :             uint32_t *ma;
    2984             :             int32_t maa;
    2985             :             uint32_t mab;
    2986             :             float *mb;
    2987             :             uint32_t n;
    2988           0 :             ma = (uint32_t *)origdata;
    2989           0 :             mb = data;
    2990           0 :             for (n = 0; n < count; n++)
    2991             :             {
    2992           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2993           0 :                     TIFFSwabLong(ma);
    2994           0 :                 maa = *(int32_t *)ma;
    2995           0 :                 ma++;
    2996           0 :                 if (tif->tif_flags & TIFF_SWAB)
    2997           0 :                     TIFFSwabLong(ma);
    2998           0 :                 mab = *ma++;
    2999           0 :                 if (mab == 0)
    3000           0 :                     *mb++ = 0.0;
    3001             :                 else
    3002           0 :                     *mb++ = (float)maa / (float)mab;
    3003             :             }
    3004             :         }
    3005           0 :         break;
    3006           0 :         case TIFF_DOUBLE:
    3007             :         {
    3008             :             double *ma;
    3009             :             float *mb;
    3010             :             uint32_t n;
    3011           0 :             if (tif->tif_flags & TIFF_SWAB)
    3012           0 :                 TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
    3013             :             TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
    3014           0 :             ma = (double *)origdata;
    3015           0 :             mb = data;
    3016           0 :             for (n = 0; n < count; n++)
    3017             :             {
    3018           0 :                 double val = *ma++;
    3019           0 :                 if (val > FLT_MAX)
    3020           0 :                     val = FLT_MAX;
    3021           0 :                 else if (val < -FLT_MAX)
    3022           0 :                     val = -FLT_MAX;
    3023           0 :                 *mb++ = (float)val;
    3024             :             }
    3025             :         }
    3026           0 :         break;
    3027           0 :         default:
    3028           0 :             break;
    3029             :     }
    3030        1027 :     _TIFFfreeExt(tif, origdata);
    3031        1027 :     *value = data;
    3032        1027 :     return (TIFFReadDirEntryErrOk);
    3033             : }
    3034             : 
    3035             : static enum TIFFReadDirEntryErr
    3036       70501 : TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
    3037             : {
    3038             :     enum TIFFReadDirEntryErr err;
    3039             :     uint32_t count;
    3040             :     void *origdata;
    3041             :     double *data;
    3042       70501 :     switch (direntry->tdir_type)
    3043             :     {
    3044       70460 :         case TIFF_BYTE:
    3045             :         case TIFF_SBYTE:
    3046             :         case TIFF_SHORT:
    3047             :         case TIFF_SSHORT:
    3048             :         case TIFF_LONG:
    3049             :         case TIFF_SLONG:
    3050             :         case TIFF_LONG8:
    3051             :         case TIFF_SLONG8:
    3052             :         case TIFF_RATIONAL:
    3053             :         case TIFF_SRATIONAL:
    3054             :         case TIFF_FLOAT:
    3055             :         case TIFF_DOUBLE:
    3056       70460 :             break;
    3057          41 :         default:
    3058          41 :             return (TIFFReadDirEntryErrType);
    3059             :     }
    3060       70460 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
    3061       70460 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    3062             :     {
    3063          50 :         *value = 0;
    3064          50 :         return (err);
    3065             :     }
    3066       70410 :     switch (direntry->tdir_type)
    3067             :     {
    3068       70421 :         case TIFF_DOUBLE:
    3069       70421 :             if (tif->tif_flags & TIFF_SWAB)
    3070         291 :                 TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
    3071             :             TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
    3072       70441 :             *value = (double *)origdata;
    3073       70441 :             return (TIFFReadDirEntryErrOk);
    3074           0 :         default:
    3075           0 :             break;
    3076             :     }
    3077           0 :     data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
    3078           0 :     if (data == 0)
    3079             :     {
    3080           0 :         _TIFFfreeExt(tif, origdata);
    3081           0 :         return (TIFFReadDirEntryErrAlloc);
    3082             :     }
    3083           0 :     switch (direntry->tdir_type)
    3084             :     {
    3085           0 :         case TIFF_BYTE:
    3086             :         {
    3087             :             uint8_t *ma;
    3088             :             double *mb;
    3089             :             uint32_t n;
    3090           0 :             ma = (uint8_t *)origdata;
    3091           0 :             mb = data;
    3092           0 :             for (n = 0; n < count; n++)
    3093           0 :                 *mb++ = (double)(*ma++);
    3094             :         }
    3095           0 :         break;
    3096           0 :         case TIFF_SBYTE:
    3097             :         {
    3098             :             int8_t *ma;
    3099             :             double *mb;
    3100             :             uint32_t n;
    3101           0 :             ma = (int8_t *)origdata;
    3102           0 :             mb = data;
    3103           0 :             for (n = 0; n < count; n++)
    3104           0 :                 *mb++ = (double)(*ma++);
    3105             :         }
    3106           0 :         break;
    3107           0 :         case TIFF_SHORT:
    3108             :         {
    3109             :             uint16_t *ma;
    3110             :             double *mb;
    3111             :             uint32_t n;
    3112           0 :             ma = (uint16_t *)origdata;
    3113           0 :             mb = data;
    3114           0 :             for (n = 0; n < count; n++)
    3115             :             {
    3116           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3117           0 :                     TIFFSwabShort(ma);
    3118           0 :                 *mb++ = (double)(*ma++);
    3119             :             }
    3120             :         }
    3121           0 :         break;
    3122           0 :         case TIFF_SSHORT:
    3123             :         {
    3124             :             int16_t *ma;
    3125             :             double *mb;
    3126             :             uint32_t n;
    3127           0 :             ma = (int16_t *)origdata;
    3128           0 :             mb = data;
    3129           0 :             for (n = 0; n < count; n++)
    3130             :             {
    3131           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3132           0 :                     TIFFSwabShort((uint16_t *)ma);
    3133           0 :                 *mb++ = (double)(*ma++);
    3134             :             }
    3135             :         }
    3136           0 :         break;
    3137           0 :         case TIFF_LONG:
    3138             :         {
    3139             :             uint32_t *ma;
    3140             :             double *mb;
    3141             :             uint32_t n;
    3142           0 :             ma = (uint32_t *)origdata;
    3143           0 :             mb = data;
    3144           0 :             for (n = 0; n < count; n++)
    3145             :             {
    3146           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3147           0 :                     TIFFSwabLong(ma);
    3148           0 :                 *mb++ = (double)(*ma++);
    3149             :             }
    3150             :         }
    3151           0 :         break;
    3152           0 :         case TIFF_SLONG:
    3153             :         {
    3154             :             int32_t *ma;
    3155             :             double *mb;
    3156             :             uint32_t n;
    3157           0 :             ma = (int32_t *)origdata;
    3158           0 :             mb = data;
    3159           0 :             for (n = 0; n < count; n++)
    3160             :             {
    3161           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3162           0 :                     TIFFSwabLong((uint32_t *)ma);
    3163           0 :                 *mb++ = (double)(*ma++);
    3164             :             }
    3165             :         }
    3166           0 :         break;
    3167           0 :         case TIFF_LONG8:
    3168             :         {
    3169             :             uint64_t *ma;
    3170             :             double *mb;
    3171             :             uint32_t n;
    3172           0 :             ma = (uint64_t *)origdata;
    3173           0 :             mb = data;
    3174           0 :             for (n = 0; n < count; n++)
    3175             :             {
    3176           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3177           0 :                     TIFFSwabLong8(ma);
    3178           0 :                 *mb++ = (double)(*ma++);
    3179             :             }
    3180             :         }
    3181           0 :         break;
    3182           0 :         case TIFF_SLONG8:
    3183             :         {
    3184             :             int64_t *ma;
    3185             :             double *mb;
    3186             :             uint32_t n;
    3187           0 :             ma = (int64_t *)origdata;
    3188           0 :             mb = data;
    3189           0 :             for (n = 0; n < count; n++)
    3190             :             {
    3191           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3192           0 :                     TIFFSwabLong8((uint64_t *)ma);
    3193           0 :                 *mb++ = (double)(*ma++);
    3194             :             }
    3195             :         }
    3196           0 :         break;
    3197           0 :         case TIFF_RATIONAL:
    3198             :         {
    3199             :             uint32_t *ma;
    3200             :             uint32_t maa;
    3201             :             uint32_t mab;
    3202             :             double *mb;
    3203             :             uint32_t n;
    3204           0 :             ma = (uint32_t *)origdata;
    3205           0 :             mb = data;
    3206           0 :             for (n = 0; n < count; n++)
    3207             :             {
    3208           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3209           0 :                     TIFFSwabLong(ma);
    3210           0 :                 maa = *ma++;
    3211           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3212           0 :                     TIFFSwabLong(ma);
    3213           0 :                 mab = *ma++;
    3214           0 :                 if (mab == 0)
    3215           0 :                     *mb++ = 0.0;
    3216             :                 else
    3217           0 :                     *mb++ = (double)maa / (double)mab;
    3218             :             }
    3219             :         }
    3220           0 :         break;
    3221           0 :         case TIFF_SRATIONAL:
    3222             :         {
    3223             :             uint32_t *ma;
    3224             :             int32_t maa;
    3225             :             uint32_t mab;
    3226             :             double *mb;
    3227             :             uint32_t n;
    3228           0 :             ma = (uint32_t *)origdata;
    3229           0 :             mb = data;
    3230           0 :             for (n = 0; n < count; n++)
    3231             :             {
    3232           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3233           0 :                     TIFFSwabLong(ma);
    3234           0 :                 maa = *(int32_t *)ma;
    3235           0 :                 ma++;
    3236           0 :                 if (tif->tif_flags & TIFF_SWAB)
    3237           0 :                     TIFFSwabLong(ma);
    3238           0 :                 mab = *ma++;
    3239           0 :                 if (mab == 0)
    3240           0 :                     *mb++ = 0.0;
    3241             :                 else
    3242           0 :                     *mb++ = (double)maa / (double)mab;
    3243             :             }
    3244             :         }
    3245           0 :         break;
    3246           0 :         case TIFF_FLOAT:
    3247             :         {
    3248             :             float *ma;
    3249             :             double *mb;
    3250             :             uint32_t n;
    3251           0 :             if (tif->tif_flags & TIFF_SWAB)
    3252           0 :                 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
    3253             :             TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
    3254           0 :             ma = (float *)origdata;
    3255           0 :             mb = data;
    3256           0 :             for (n = 0; n < count; n++)
    3257           0 :                 *mb++ = (double)(*ma++);
    3258             :         }
    3259           0 :         break;
    3260           0 :         default:
    3261           0 :             break;
    3262             :     }
    3263           0 :     _TIFFfreeExt(tif, origdata);
    3264           0 :     *value = data;
    3265           0 :     return (TIFFReadDirEntryErrOk);
    3266             : }
    3267             : 
    3268             : static enum TIFFReadDirEntryErr
    3269          81 : TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
    3270             : {
    3271             :     enum TIFFReadDirEntryErr err;
    3272             :     uint32_t count;
    3273             :     void *origdata;
    3274             :     uint64_t *data;
    3275          81 :     switch (direntry->tdir_type)
    3276             :     {
    3277          81 :         case TIFF_LONG:
    3278             :         case TIFF_LONG8:
    3279             :         case TIFF_IFD:
    3280             :         case TIFF_IFD8:
    3281          81 :             break;
    3282           0 :         default:
    3283           0 :             return (TIFFReadDirEntryErrType);
    3284             :     }
    3285          81 :     err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
    3286          81 :     if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    3287             :     {
    3288           0 :         *value = 0;
    3289           0 :         return (err);
    3290             :     }
    3291          81 :     switch (direntry->tdir_type)
    3292             :     {
    3293           0 :         case TIFF_LONG8:
    3294             :         case TIFF_IFD8:
    3295           0 :             *value = (uint64_t *)origdata;
    3296           0 :             if (tif->tif_flags & TIFF_SWAB)
    3297           0 :                 TIFFSwabArrayOfLong8(*value, count);
    3298           0 :             return (TIFFReadDirEntryErrOk);
    3299          81 :         default:
    3300          81 :             break;
    3301             :     }
    3302          81 :     data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
    3303          81 :     if (data == 0)
    3304             :     {
    3305           0 :         _TIFFfreeExt(tif, origdata);
    3306           0 :         return (TIFFReadDirEntryErrAlloc);
    3307             :     }
    3308          81 :     switch (direntry->tdir_type)
    3309             :     {
    3310          81 :         case TIFF_LONG:
    3311             :         case TIFF_IFD:
    3312             :         {
    3313             :             uint32_t *ma;
    3314             :             uint64_t *mb;
    3315             :             uint32_t n;
    3316          81 :             ma = (uint32_t *)origdata;
    3317          81 :             mb = data;
    3318         333 :             for (n = 0; n < count; n++)
    3319             :             {
    3320         252 :                 if (tif->tif_flags & TIFF_SWAB)
    3321           0 :                     TIFFSwabLong(ma);
    3322         252 :                 *mb++ = (uint64_t)(*ma++);
    3323             :             }
    3324             :         }
    3325          81 :         break;
    3326           0 :         default:
    3327           0 :             break;
    3328             :     }
    3329          81 :     _TIFFfreeExt(tif, origdata);
    3330          81 :     *value = data;
    3331          81 :     return (TIFFReadDirEntryErrOk);
    3332             : }
    3333             : 
    3334             : static enum TIFFReadDirEntryErr
    3335       32704 : TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
    3336             :                                uint16_t *value)
    3337             : {
    3338             :     enum TIFFReadDirEntryErr err;
    3339             :     uint16_t *m;
    3340             :     uint16_t *na;
    3341             :     uint16_t nb;
    3342       32704 :     if (direntry->tdir_count != (uint64_t)tif->tif_dir.td_samplesperpixel)
    3343             :     {
    3344           0 :         const TIFFField *fip = TIFFFieldWithTag(tif, direntry->tdir_tag);
    3345           0 :         if (direntry->tdir_count == 0)
    3346             :         {
    3347           0 :             return TIFFReadDirEntryErrCount;
    3348             :         }
    3349           0 :         else if (direntry->tdir_count <
    3350           0 :                  (uint64_t)tif->tif_dir.td_samplesperpixel)
    3351             :         {
    3352           0 :             TIFFWarningExtR(
    3353             :                 tif, "TIFFReadDirEntryPersampleShort",
    3354             :                 "Tag %s entry count is %" PRIu64
    3355             :                 " , whereas it should be SamplesPerPixel=%d. Assuming that "
    3356             :                 "missing entries are all at the value of the first one",
    3357             :                 fip ? fip->field_name : "unknown tagname", direntry->tdir_count,
    3358           0 :                 tif->tif_dir.td_samplesperpixel);
    3359             :         }
    3360             :         else
    3361             :         {
    3362           0 :             TIFFWarningExtR(tif, "TIFFReadDirEntryPersampleShort",
    3363             :                             "Tag %s entry count is %" PRIu64
    3364             :                             " , whereas it should be SamplesPerPixel=%d. "
    3365             :                             "Ignoring extra entries",
    3366             :                             fip ? fip->field_name : "unknown tagname",
    3367             :                             direntry->tdir_count,
    3368           0 :                             tif->tif_dir.td_samplesperpixel);
    3369             :         }
    3370             :     }
    3371       32704 :     err = TIFFReadDirEntryShortArray(tif, direntry, &m);
    3372       32708 :     if (err != TIFFReadDirEntryErrOk || m == NULL)
    3373           4 :         return (err);
    3374       32704 :     na = m;
    3375       32704 :     nb = tif->tif_dir.td_samplesperpixel;
    3376       32704 :     if (direntry->tdir_count < nb)
    3377           0 :         nb = (uint16_t)direntry->tdir_count;
    3378       32704 :     *value = *na++;
    3379       32704 :     nb--;
    3380     2593180 :     while (nb > 0)
    3381             :     {
    3382     2560470 :         if (*na++ != *value)
    3383             :         {
    3384           0 :             err = TIFFReadDirEntryErrPsdif;
    3385           0 :             break;
    3386             :         }
    3387     2560470 :         nb--;
    3388             :     }
    3389       32704 :     _TIFFfreeExt(tif, m);
    3390       32706 :     return (err);
    3391             : }
    3392             : 
    3393           0 : static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
    3394             :                                         uint8_t *value)
    3395             : {
    3396             :     (void)tif;
    3397           0 :     *value = *(uint8_t *)(&direntry->tdir_offset);
    3398           0 : }
    3399             : 
    3400           0 : static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
    3401             :                                          int8_t *value)
    3402             : {
    3403             :     (void)tif;
    3404           0 :     *value = *(int8_t *)(&direntry->tdir_offset);
    3405           0 : }
    3406             : 
    3407      467738 : static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
    3408             :                                          uint16_t *value)
    3409             : {
    3410      467738 :     *value = direntry->tdir_offset.toff_short;
    3411             :     /* *value=*(uint16_t*)(&direntry->tdir_offset); */
    3412      467738 :     if (tif->tif_flags & TIFF_SWAB)
    3413        4781 :         TIFFSwabShort(value);
    3414      467738 : }
    3415             : 
    3416           0 : static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
    3417             :                                           int16_t *value)
    3418             : {
    3419           0 :     *value = *(int16_t *)(&direntry->tdir_offset);
    3420           0 :     if (tif->tif_flags & TIFF_SWAB)
    3421           0 :         TIFFSwabShort((uint16_t *)value);
    3422           0 : }
    3423             : 
    3424        5877 : static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
    3425             :                                         uint32_t *value)
    3426             : {
    3427        5877 :     *value = *(uint32_t *)(&direntry->tdir_offset);
    3428        5877 :     if (tif->tif_flags & TIFF_SWAB)
    3429         114 :         TIFFSwabLong(value);
    3430        5877 : }
    3431             : 
    3432           0 : static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
    3433             :                                          int32_t *value)
    3434             : {
    3435           0 :     *value = *(int32_t *)(&direntry->tdir_offset);
    3436           0 :     if (tif->tif_flags & TIFF_SWAB)
    3437           0 :         TIFFSwabLong((uint32_t *)value);
    3438           0 : }
    3439             : 
    3440             : static enum TIFFReadDirEntryErr
    3441           0 : TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
    3442             : {
    3443           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3444             :     {
    3445             :         enum TIFFReadDirEntryErr err;
    3446           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3447           0 :         if (tif->tif_flags & TIFF_SWAB)
    3448           0 :             TIFFSwabLong(&offset);
    3449           0 :         err = TIFFReadDirEntryData(tif, offset, 8, value);
    3450           0 :         if (err != TIFFReadDirEntryErrOk)
    3451           0 :             return (err);
    3452             :     }
    3453             :     else
    3454           0 :         *value = direntry->tdir_offset.toff_long8;
    3455           0 :     if (tif->tif_flags & TIFF_SWAB)
    3456           0 :         TIFFSwabLong8(value);
    3457           0 :     return (TIFFReadDirEntryErrOk);
    3458             : }
    3459             : 
    3460             : static enum TIFFReadDirEntryErr
    3461           0 : TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
    3462             : {
    3463           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3464             :     {
    3465             :         enum TIFFReadDirEntryErr err;
    3466           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3467           0 :         if (tif->tif_flags & TIFF_SWAB)
    3468           0 :             TIFFSwabLong(&offset);
    3469           0 :         err = TIFFReadDirEntryData(tif, offset, 8, value);
    3470           0 :         if (err != TIFFReadDirEntryErrOk)
    3471           0 :             return (err);
    3472             :     }
    3473             :     else
    3474           0 :         *value = *(int64_t *)(&direntry->tdir_offset);
    3475           0 :     if (tif->tif_flags & TIFF_SWAB)
    3476           0 :         TIFFSwabLong8((uint64_t *)value);
    3477           0 :     return (TIFFReadDirEntryErrOk);
    3478             : }
    3479             : 
    3480             : static enum TIFFReadDirEntryErr
    3481         248 : TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
    3482             :                                 double *value)
    3483             : {
    3484             :     UInt64Aligned_t m;
    3485             : 
    3486             :     assert(sizeof(double) == 8);
    3487             :     assert(sizeof(uint64_t) == 8);
    3488             :     assert(sizeof(uint32_t) == 4);
    3489         248 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3490             :     {
    3491             :         enum TIFFReadDirEntryErr err;
    3492         248 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3493         248 :         if (tif->tif_flags & TIFF_SWAB)
    3494           8 :             TIFFSwabLong(&offset);
    3495         248 :         err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    3496         248 :         if (err != TIFFReadDirEntryErrOk)
    3497           0 :             return (err);
    3498             :     }
    3499             :     else
    3500           0 :         m.l = direntry->tdir_offset.toff_long8;
    3501         248 :     if (tif->tif_flags & TIFF_SWAB)
    3502           8 :         TIFFSwabArrayOfLong(m.i, 2);
    3503             :     /* Not completely sure what we should do when m.i[1]==0, but some */
    3504             :     /* sanitizers do not like division by 0.0: */
    3505             :     /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
    3506         248 :     if (m.i[0] == 0 || m.i[1] == 0)
    3507          36 :         *value = 0.0;
    3508             :     else
    3509         212 :         *value = (double)m.i[0] / (double)m.i[1];
    3510         248 :     return (TIFFReadDirEntryErrOk);
    3511             : }
    3512             : 
    3513             : static enum TIFFReadDirEntryErr
    3514           0 : TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
    3515             :                                  double *value)
    3516             : {
    3517             :     UInt64Aligned_t m;
    3518             :     assert(sizeof(double) == 8);
    3519             :     assert(sizeof(uint64_t) == 8);
    3520             :     assert(sizeof(int32_t) == 4);
    3521             :     assert(sizeof(uint32_t) == 4);
    3522           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3523             :     {
    3524             :         enum TIFFReadDirEntryErr err;
    3525           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3526           0 :         if (tif->tif_flags & TIFF_SWAB)
    3527           0 :             TIFFSwabLong(&offset);
    3528           0 :         err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    3529           0 :         if (err != TIFFReadDirEntryErrOk)
    3530           0 :             return (err);
    3531             :     }
    3532             :     else
    3533           0 :         m.l = direntry->tdir_offset.toff_long8;
    3534           0 :     if (tif->tif_flags & TIFF_SWAB)
    3535           0 :         TIFFSwabArrayOfLong(m.i, 2);
    3536             :     /* Not completely sure what we should do when m.i[1]==0, but some */
    3537             :     /* sanitizers do not like division by 0.0: */
    3538             :     /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
    3539           0 :     if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
    3540           0 :         *value = 0.0;
    3541             :     else
    3542           0 :         *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
    3543           0 :     return (TIFFReadDirEntryErrOk);
    3544             : }
    3545             : 
    3546             : #if 0
    3547             : static enum TIFFReadDirEntryErr
    3548             : TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
    3549             :                                       TIFFRational_t *value)
    3550             : { /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
    3551             :      directly --*/
    3552             :     UInt64Aligned_t m;
    3553             : 
    3554             :     assert(sizeof(double) == 8);
    3555             :     assert(sizeof(uint64_t) == 8);
    3556             :     assert(sizeof(uint32_t) == 4);
    3557             : 
    3558             :     if (direntry->tdir_count != 1)
    3559             :         return (TIFFReadDirEntryErrCount);
    3560             : 
    3561             :     if (direntry->tdir_type != TIFF_RATIONAL &&
    3562             :         direntry->tdir_type != TIFF_SRATIONAL)
    3563             :         return (TIFFReadDirEntryErrType);
    3564             : 
    3565             :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3566             :     {
    3567             :         enum TIFFReadDirEntryErr err;
    3568             :         uint32_t offset = direntry->tdir_offset.toff_long;
    3569             :         if (tif->tif_flags & TIFF_SWAB)
    3570             :             TIFFSwabLong(&offset);
    3571             :         err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    3572             :         if (err != TIFFReadDirEntryErrOk)
    3573             :             return (err);
    3574             :     }
    3575             :     else
    3576             :     {
    3577             :         m.l = direntry->tdir_offset.toff_long8;
    3578             :     }
    3579             : 
    3580             :     if (tif->tif_flags & TIFF_SWAB)
    3581             :         TIFFSwabArrayOfLong(m.i, 2);
    3582             : 
    3583             :     value->uNum = m.i[0];
    3584             :     value->uDenom = m.i[1];
    3585             :     return (TIFFReadDirEntryErrOk);
    3586             : } /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
    3587             : #endif
    3588             : 
    3589           0 : static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
    3590             :                                          float *value)
    3591             : {
    3592             :     union
    3593             :     {
    3594             :         float f;
    3595             :         uint32_t i;
    3596             :     } float_union;
    3597             :     assert(sizeof(float) == 4);
    3598             :     assert(sizeof(uint32_t) == 4);
    3599             :     assert(sizeof(float_union) == 4);
    3600           0 :     float_union.i = *(uint32_t *)(&direntry->tdir_offset);
    3601           0 :     *value = float_union.f;
    3602           0 :     if (tif->tif_flags & TIFF_SWAB)
    3603           0 :         TIFFSwabLong((uint32_t *)value);
    3604           0 : }
    3605             : 
    3606             : static enum TIFFReadDirEntryErr
    3607           0 : TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
    3608             : {
    3609             :     assert(sizeof(double) == 8);
    3610             :     assert(sizeof(uint64_t) == 8);
    3611             :     assert(sizeof(UInt64Aligned_t) == 8);
    3612           0 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    3613             :     {
    3614             :         enum TIFFReadDirEntryErr err;
    3615           0 :         uint32_t offset = direntry->tdir_offset.toff_long;
    3616           0 :         if (tif->tif_flags & TIFF_SWAB)
    3617           0 :             TIFFSwabLong(&offset);
    3618           0 :         err = TIFFReadDirEntryData(tif, offset, 8, value);
    3619           0 :         if (err != TIFFReadDirEntryErrOk)
    3620           0 :             return (err);
    3621             :     }
    3622             :     else
    3623             :     {
    3624             :         UInt64Aligned_t uint64_union;
    3625           0 :         uint64_union.l = direntry->tdir_offset.toff_long8;
    3626           0 :         *value = uint64_union.d;
    3627             :     }
    3628           0 :     if (tif->tif_flags & TIFF_SWAB)
    3629           0 :         TIFFSwabLong8((uint64_t *)value);
    3630           0 :     return (TIFFReadDirEntryErrOk);
    3631             : }
    3632             : 
    3633             : static enum TIFFReadDirEntryErr
    3634           0 : TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
    3635             : {
    3636           0 :     if (value < 0)
    3637           0 :         return (TIFFReadDirEntryErrRange);
    3638             :     else
    3639           0 :         return (TIFFReadDirEntryErrOk);
    3640             : }
    3641             : 
    3642             : static enum TIFFReadDirEntryErr
    3643           0 : TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
    3644             : {
    3645           0 :     if (value > 0xFF)
    3646           0 :         return (TIFFReadDirEntryErrRange);
    3647             :     else
    3648           0 :         return (TIFFReadDirEntryErrOk);
    3649             : }
    3650             : 
    3651             : static enum TIFFReadDirEntryErr
    3652           0 : TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
    3653             : {
    3654           0 :     if ((value < 0) || (value > 0xFF))
    3655           0 :         return (TIFFReadDirEntryErrRange);
    3656             :     else
    3657           0 :         return (TIFFReadDirEntryErrOk);
    3658             : }
    3659             : 
    3660             : static enum TIFFReadDirEntryErr
    3661           0 : TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
    3662             : {
    3663           0 :     if (value > 0xFF)
    3664           0 :         return (TIFFReadDirEntryErrRange);
    3665             :     else
    3666           0 :         return (TIFFReadDirEntryErrOk);
    3667             : }
    3668             : 
    3669             : static enum TIFFReadDirEntryErr
    3670           0 : TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
    3671             : {
    3672           0 :     if ((value < 0) || (value > 0xFF))
    3673           0 :         return (TIFFReadDirEntryErrRange);
    3674             :     else
    3675           0 :         return (TIFFReadDirEntryErrOk);
    3676             : }
    3677             : 
    3678             : static enum TIFFReadDirEntryErr
    3679           0 : TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
    3680             : {
    3681           0 :     if (value > 0xFF)
    3682           0 :         return (TIFFReadDirEntryErrRange);
    3683             :     else
    3684           0 :         return (TIFFReadDirEntryErrOk);
    3685             : }
    3686             : 
    3687             : static enum TIFFReadDirEntryErr
    3688           0 : TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
    3689             : {
    3690           0 :     if ((value < 0) || (value > 0xFF))
    3691           0 :         return (TIFFReadDirEntryErrRange);
    3692             :     else
    3693           0 :         return (TIFFReadDirEntryErrOk);
    3694             : }
    3695             : 
    3696             : static enum TIFFReadDirEntryErr
    3697           0 : TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
    3698             : {
    3699           0 :     if (value > 0x7F)
    3700           0 :         return (TIFFReadDirEntryErrRange);
    3701             :     else
    3702           0 :         return (TIFFReadDirEntryErrOk);
    3703             : }
    3704             : 
    3705             : static enum TIFFReadDirEntryErr
    3706           0 : TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
    3707             : {
    3708           0 :     if (value > 0x7F)
    3709           0 :         return (TIFFReadDirEntryErrRange);
    3710             :     else
    3711           0 :         return (TIFFReadDirEntryErrOk);
    3712             : }
    3713             : 
    3714             : static enum TIFFReadDirEntryErr
    3715           0 : TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
    3716             : {
    3717           0 :     if ((value < -0x80) || (value > 0x7F))
    3718           0 :         return (TIFFReadDirEntryErrRange);
    3719             :     else
    3720           0 :         return (TIFFReadDirEntryErrOk);
    3721             : }
    3722             : 
    3723             : static enum TIFFReadDirEntryErr
    3724           0 : TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
    3725             : {
    3726           0 :     if (value > 0x7F)
    3727           0 :         return (TIFFReadDirEntryErrRange);
    3728             :     else
    3729           0 :         return (TIFFReadDirEntryErrOk);
    3730             : }
    3731             : 
    3732             : static enum TIFFReadDirEntryErr
    3733           0 : TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
    3734             : {
    3735           0 :     if ((value < -0x80) || (value > 0x7F))
    3736           0 :         return (TIFFReadDirEntryErrRange);
    3737             :     else
    3738           0 :         return (TIFFReadDirEntryErrOk);
    3739             : }
    3740             : 
    3741             : static enum TIFFReadDirEntryErr
    3742           0 : TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
    3743             : {
    3744           0 :     if (value > 0x7F)
    3745           0 :         return (TIFFReadDirEntryErrRange);
    3746             :     else
    3747           0 :         return (TIFFReadDirEntryErrOk);
    3748             : }
    3749             : 
    3750             : static enum TIFFReadDirEntryErr
    3751           0 : TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
    3752             : {
    3753           0 :     if ((value < -0x80) || (value > 0x7F))
    3754           0 :         return (TIFFReadDirEntryErrRange);
    3755             :     else
    3756           0 :         return (TIFFReadDirEntryErrOk);
    3757             : }
    3758             : 
    3759             : static enum TIFFReadDirEntryErr
    3760           0 : TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
    3761             : {
    3762           0 :     if (value < 0)
    3763           0 :         return (TIFFReadDirEntryErrRange);
    3764             :     else
    3765           0 :         return (TIFFReadDirEntryErrOk);
    3766             : }
    3767             : 
    3768             : static enum TIFFReadDirEntryErr
    3769           0 : TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
    3770             : {
    3771           0 :     if (value < 0)
    3772           0 :         return (TIFFReadDirEntryErrRange);
    3773             :     else
    3774           0 :         return (TIFFReadDirEntryErrOk);
    3775             : }
    3776             : 
    3777             : static enum TIFFReadDirEntryErr
    3778           1 : TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
    3779             : {
    3780           1 :     if (value > 0xFFFF)
    3781           0 :         return (TIFFReadDirEntryErrRange);
    3782             :     else
    3783           1 :         return (TIFFReadDirEntryErrOk);
    3784             : }
    3785             : 
    3786             : static enum TIFFReadDirEntryErr
    3787           0 : TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
    3788             : {
    3789           0 :     if ((value < 0) || (value > 0xFFFF))
    3790           0 :         return (TIFFReadDirEntryErrRange);
    3791             :     else
    3792           0 :         return (TIFFReadDirEntryErrOk);
    3793             : }
    3794             : 
    3795             : static enum TIFFReadDirEntryErr
    3796           0 : TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
    3797             : {
    3798           0 :     if (value > 0xFFFF)
    3799           0 :         return (TIFFReadDirEntryErrRange);
    3800             :     else
    3801           0 :         return (TIFFReadDirEntryErrOk);
    3802             : }
    3803             : 
    3804             : static enum TIFFReadDirEntryErr
    3805           0 : TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
    3806             : {
    3807           0 :     if ((value < 0) || (value > 0xFFFF))
    3808           0 :         return (TIFFReadDirEntryErrRange);
    3809             :     else
    3810           0 :         return (TIFFReadDirEntryErrOk);
    3811             : }
    3812             : 
    3813             : static enum TIFFReadDirEntryErr
    3814           0 : TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
    3815             : {
    3816           0 :     if (value > 0x7FFF)
    3817           0 :         return (TIFFReadDirEntryErrRange);
    3818             :     else
    3819           0 :         return (TIFFReadDirEntryErrOk);
    3820             : }
    3821             : 
    3822             : static enum TIFFReadDirEntryErr
    3823           0 : TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
    3824             : {
    3825           0 :     if (value > 0x7FFF)
    3826           0 :         return (TIFFReadDirEntryErrRange);
    3827             :     else
    3828           0 :         return (TIFFReadDirEntryErrOk);
    3829             : }
    3830             : 
    3831             : static enum TIFFReadDirEntryErr
    3832           0 : TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
    3833             : {
    3834           0 :     if ((value < -0x8000) || (value > 0x7FFF))
    3835           0 :         return (TIFFReadDirEntryErrRange);
    3836             :     else
    3837           0 :         return (TIFFReadDirEntryErrOk);
    3838             : }
    3839             : 
    3840             : static enum TIFFReadDirEntryErr
    3841           0 : TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
    3842             : {
    3843           0 :     if (value > 0x7FFF)
    3844           0 :         return (TIFFReadDirEntryErrRange);
    3845             :     else
    3846           0 :         return (TIFFReadDirEntryErrOk);
    3847             : }
    3848             : 
    3849             : static enum TIFFReadDirEntryErr
    3850           0 : TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
    3851             : {
    3852           0 :     if ((value < -0x8000) || (value > 0x7FFF))
    3853           0 :         return (TIFFReadDirEntryErrRange);
    3854             :     else
    3855           0 :         return (TIFFReadDirEntryErrOk);
    3856             : }
    3857             : 
    3858             : static enum TIFFReadDirEntryErr
    3859           0 : TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
    3860             : {
    3861           0 :     if (value < 0)
    3862           0 :         return (TIFFReadDirEntryErrRange);
    3863             :     else
    3864           0 :         return (TIFFReadDirEntryErrOk);
    3865             : }
    3866             : 
    3867             : static enum TIFFReadDirEntryErr
    3868           0 : TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
    3869             : {
    3870           0 :     if (value < 0)
    3871           0 :         return (TIFFReadDirEntryErrRange);
    3872             :     else
    3873           0 :         return (TIFFReadDirEntryErrOk);
    3874             : }
    3875             : 
    3876             : static enum TIFFReadDirEntryErr
    3877           0 : TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
    3878             : {
    3879           0 :     if (value < 0)
    3880           0 :         return (TIFFReadDirEntryErrRange);
    3881             :     else
    3882           0 :         return (TIFFReadDirEntryErrOk);
    3883             : }
    3884             : 
    3885             : static enum TIFFReadDirEntryErr
    3886           0 : TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
    3887             : {
    3888           0 :     if (value > UINT32_MAX)
    3889           0 :         return (TIFFReadDirEntryErrRange);
    3890             :     else
    3891           0 :         return (TIFFReadDirEntryErrOk);
    3892             : }
    3893             : 
    3894             : static enum TIFFReadDirEntryErr
    3895           0 : TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
    3896             : {
    3897           0 :     if ((value < 0) || (value > (int64_t)UINT32_MAX))
    3898           0 :         return (TIFFReadDirEntryErrRange);
    3899             :     else
    3900           0 :         return (TIFFReadDirEntryErrOk);
    3901             : }
    3902             : 
    3903             : static enum TIFFReadDirEntryErr
    3904           0 : TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
    3905             : {
    3906           0 :     if (value > 0x7FFFFFFFUL)
    3907           0 :         return (TIFFReadDirEntryErrRange);
    3908             :     else
    3909           0 :         return (TIFFReadDirEntryErrOk);
    3910             : }
    3911             : 
    3912             : /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
    3913             : static enum TIFFReadDirEntryErr
    3914           0 : TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
    3915             : {
    3916           0 :     if (value > 0x7FFFFFFF)
    3917           0 :         return (TIFFReadDirEntryErrRange);
    3918             :     else
    3919           0 :         return (TIFFReadDirEntryErrOk);
    3920             : }
    3921             : 
    3922             : /* Check that the 8-byte signed value can fit in a 4-byte signed range */
    3923             : static enum TIFFReadDirEntryErr
    3924           0 : TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
    3925             : {
    3926           0 :     if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
    3927           0 :         return (TIFFReadDirEntryErrRange);
    3928             :     else
    3929           0 :         return (TIFFReadDirEntryErrOk);
    3930             : }
    3931             : 
    3932             : static enum TIFFReadDirEntryErr
    3933           0 : TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
    3934             : {
    3935           0 :     if (value < 0)
    3936           0 :         return (TIFFReadDirEntryErrRange);
    3937             :     else
    3938           0 :         return (TIFFReadDirEntryErrOk);
    3939             : }
    3940             : 
    3941             : static enum TIFFReadDirEntryErr
    3942           0 : TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
    3943             : {
    3944           0 :     if (value < 0)
    3945           0 :         return (TIFFReadDirEntryErrRange);
    3946             :     else
    3947           0 :         return (TIFFReadDirEntryErrOk);
    3948             : }
    3949             : 
    3950             : static enum TIFFReadDirEntryErr
    3951           0 : TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
    3952             : {
    3953           0 :     if (value < 0)
    3954           0 :         return (TIFFReadDirEntryErrRange);
    3955             :     else
    3956           0 :         return (TIFFReadDirEntryErrOk);
    3957             : }
    3958             : 
    3959             : static enum TIFFReadDirEntryErr
    3960           1 : TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
    3961             : {
    3962           1 :     if (value < 0)
    3963           0 :         return (TIFFReadDirEntryErrRange);
    3964             :     else
    3965           1 :         return (TIFFReadDirEntryErrOk);
    3966             : }
    3967             : 
    3968             : static enum TIFFReadDirEntryErr
    3969           0 : TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
    3970             : {
    3971           0 :     if (value > INT64_MAX)
    3972           0 :         return (TIFFReadDirEntryErrRange);
    3973             :     else
    3974           0 :         return (TIFFReadDirEntryErrOk);
    3975             : }
    3976             : 
    3977         280 : static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
    3978             :                                                      tmsize_t size, void *dest)
    3979             : {
    3980         280 :     assert(size > 0);
    3981         280 :     if (!isMapped(tif))
    3982             :     {
    3983         248 :         if (!SeekOK(tif, offset))
    3984           0 :             return (TIFFReadDirEntryErrIo);
    3985         248 :         if (!ReadOK(tif, dest, size))
    3986           0 :             return (TIFFReadDirEntryErrIo);
    3987             :     }
    3988             :     else
    3989             :     {
    3990             :         size_t ma, mb;
    3991          32 :         ma = (size_t)offset;
    3992          32 :         if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
    3993             :         {
    3994           0 :             return TIFFReadDirEntryErrIo;
    3995             :         }
    3996          32 :         mb = ma + size;
    3997          32 :         if (mb > (uint64_t)tif->tif_size)
    3998           0 :             return (TIFFReadDirEntryErrIo);
    3999          32 :         _TIFFmemcpy(dest, tif->tif_base + ma, size);
    4000             :     }
    4001         280 :     return (TIFFReadDirEntryErrOk);
    4002             : }
    4003             : 
    4004          43 : static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
    4005             :                                       const char *module, const char *tagname,
    4006             :                                       int recover)
    4007             : {
    4008          43 :     if (!recover)
    4009             :     {
    4010           2 :         switch (err)
    4011             :         {
    4012           0 :             case TIFFReadDirEntryErrCount:
    4013           0 :                 TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
    4014             :                               tagname);
    4015           0 :                 break;
    4016           0 :             case TIFFReadDirEntryErrType:
    4017           0 :                 TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
    4018             :                               tagname);
    4019           0 :                 break;
    4020           2 :             case TIFFReadDirEntryErrIo:
    4021           2 :                 TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
    4022             :                               tagname);
    4023           2 :                 break;
    4024           0 :             case TIFFReadDirEntryErrRange:
    4025           0 :                 TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
    4026             :                               tagname);
    4027           0 :                 break;
    4028           0 :             case TIFFReadDirEntryErrPsdif:
    4029           0 :                 TIFFErrorExtR(
    4030             :                     tif, module,
    4031             :                     "Cannot handle different values per sample for \"%s\"",
    4032             :                     tagname);
    4033           0 :                 break;
    4034           0 :             case TIFFReadDirEntryErrSizesan:
    4035           0 :                 TIFFErrorExtR(tif, module,
    4036             :                               "Sanity check on size of \"%s\" value failed",
    4037             :                               tagname);
    4038           0 :                 break;
    4039           0 :             case TIFFReadDirEntryErrAlloc:
    4040           0 :                 TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
    4041             :                               tagname);
    4042           0 :                 break;
    4043           0 :             case TIFFReadDirEntryErrOk:
    4044             :             default:
    4045           0 :                 assert(0); /* we should never get here */
    4046             :                 break;
    4047             :         }
    4048             :     }
    4049             :     else
    4050             :     {
    4051          41 :         switch (err)
    4052             :         {
    4053           4 :             case TIFFReadDirEntryErrCount:
    4054           4 :                 TIFFWarningExtR(tif, module,
    4055             :                                 "Incorrect count for \"%s\"; tag ignored",
    4056             :                                 tagname);
    4057           4 :                 break;
    4058           0 :             case TIFFReadDirEntryErrType:
    4059           0 :                 TIFFWarningExtR(tif, module,
    4060             :                                 "Incompatible type for \"%s\"; tag ignored",
    4061             :                                 tagname);
    4062           0 :                 break;
    4063          19 :             case TIFFReadDirEntryErrIo:
    4064          19 :                 TIFFWarningExtR(
    4065             :                     tif, module,
    4066             :                     "IO error during reading of \"%s\"; tag ignored", tagname);
    4067          19 :                 break;
    4068           0 :             case TIFFReadDirEntryErrRange:
    4069           0 :                 TIFFWarningExtR(tif, module,
    4070             :                                 "Incorrect value for \"%s\"; tag ignored",
    4071             :                                 tagname);
    4072           0 :                 break;
    4073           0 :             case TIFFReadDirEntryErrPsdif:
    4074           0 :                 TIFFWarningExtR(tif, module,
    4075             :                                 "Cannot handle different values per sample for "
    4076             :                                 "\"%s\"; tag ignored",
    4077             :                                 tagname);
    4078           0 :                 break;
    4079           3 :             case TIFFReadDirEntryErrSizesan:
    4080           3 :                 TIFFWarningExtR(
    4081             :                     tif, module,
    4082             :                     "Sanity check on size of \"%s\" value failed; tag ignored",
    4083             :                     tagname);
    4084           3 :                 break;
    4085          15 :             case TIFFReadDirEntryErrAlloc:
    4086          15 :                 TIFFWarningExtR(tif, module,
    4087             :                                 "Out of memory reading of \"%s\"; tag ignored",
    4088             :                                 tagname);
    4089          15 :                 break;
    4090           0 :             case TIFFReadDirEntryErrOk:
    4091             :             default:
    4092           0 :                 assert(0); /* we should never get here */
    4093             :                 break;
    4094             :         }
    4095             :     }
    4096          43 : }
    4097             : 
    4098             : /*
    4099             :  * Return the maximum number of color channels specified for a given photometric
    4100             :  * type. 0 is returned if photometric type isn't supported or no default value
    4101             :  * is defined by the specification.
    4102             :  */
    4103       54068 : static int _TIFFGetMaxColorChannels(uint16_t photometric)
    4104             : {
    4105       54068 :     switch (photometric)
    4106             :     {
    4107       38972 :         case PHOTOMETRIC_PALETTE:
    4108             :         case PHOTOMETRIC_MINISWHITE:
    4109             :         case PHOTOMETRIC_MINISBLACK:
    4110       38972 :             return 1;
    4111       14350 :         case PHOTOMETRIC_YCBCR:
    4112             :         case PHOTOMETRIC_RGB:
    4113             :         case PHOTOMETRIC_CIELAB:
    4114             :         case PHOTOMETRIC_LOGLUV:
    4115             :         case PHOTOMETRIC_ITULAB:
    4116             :         case PHOTOMETRIC_ICCLAB:
    4117       14350 :             return 3;
    4118         750 :         case PHOTOMETRIC_SEPARATED:
    4119             :         case PHOTOMETRIC_MASK:
    4120         750 :             return 4;
    4121           0 :         case PHOTOMETRIC_LOGL:
    4122             :         case PHOTOMETRIC_CFA:
    4123             :         default:
    4124           0 :             return 0;
    4125             :     }
    4126             : }
    4127             : 
    4128       33607 : static int ByteCountLooksBad(TIFF *tif)
    4129             : {
    4130             :     /*
    4131             :      * Assume we have wrong StripByteCount value (in case
    4132             :      * of single strip) in following cases:
    4133             :      *   - it is equal to zero along with StripOffset;
    4134             :      *   - it is larger than file itself (in case of uncompressed
    4135             :      *     image);
    4136             :      *   - it is smaller than the size of the bytes per row
    4137             :      *     multiplied on the number of rows.  The last case should
    4138             :      *     not be checked in the case of writing new image,
    4139             :      *     because we may do not know the exact strip size
    4140             :      *     until the whole image will be written and directory
    4141             :      *     dumped out.
    4142             :      */
    4143       33607 :     uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
    4144       33636 :     uint64_t offset = TIFFGetStrileOffset(tif, 0);
    4145             :     uint64_t filesize;
    4146             : 
    4147       33623 :     if (offset == 0)
    4148       10940 :         return 0;
    4149       22683 :     if (bytecount == 0)
    4150           1 :         return 1;
    4151       22682 :     if (tif->tif_dir.td_compression != COMPRESSION_NONE)
    4152         763 :         return 0;
    4153       21919 :     filesize = TIFFGetFileSize(tif);
    4154       21955 :     if (offset <= filesize && bytecount > filesize - offset)
    4155           2 :         return 1;
    4156       21953 :     if (tif->tif_mode == O_RDONLY)
    4157             :     {
    4158       20829 :         uint64_t scanlinesize = TIFFScanlineSize64(tif);
    4159       20809 :         if (tif->tif_dir.td_imagelength > 0 &&
    4160       20798 :             scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
    4161             :         {
    4162           0 :             return 1;
    4163             :         }
    4164       20809 :         if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
    4165           2 :             return 1;
    4166             :     }
    4167       21931 :     return 0;
    4168             : }
    4169             : 
    4170             : /*
    4171             :  * To evaluate the IFD data size when reading, save the offset and data size of
    4172             :  * all data that does not fit into the IFD entries themselves.
    4173             :  */
    4174      362781 : static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp)
    4175             : {
    4176      362781 :     const uint64_t data_width = TIFFDataWidth((TIFFDataType)dp->tdir_type);
    4177      362551 :     if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
    4178             :     {
    4179           1 :         TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
    4180             :                       "Too large IFD data size");
    4181           1 :         return false;
    4182             :     }
    4183      362550 :     const uint64_t datalength = dp->tdir_count * data_width;
    4184      362550 :     if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
    4185             :     {
    4186      198236 :         if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
    4187             :         {
    4188           0 :             TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
    4189             :                           "Too large IFD data size");
    4190           0 :             return false;
    4191             :         }
    4192      198236 :         tif->tif_dir.td_dirdatasize_read += datalength;
    4193      198236 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    4194             :         {
    4195             :             /* The offset of TIFFDirEntry are not swapped when read in. That has
    4196             :              * to be done when used. */
    4197      196705 :             uint32_t offset = dp->tdir_offset.toff_long;
    4198      196705 :             if (tif->tif_flags & TIFF_SWAB)
    4199        2100 :                 TIFFSwabLong(&offset);
    4200             :             tif->tif_dir
    4201      196723 :                 .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
    4202      196723 :                 .offset = (uint64_t)offset;
    4203             :         }
    4204             :         else
    4205             :         {
    4206             :             tif->tif_dir
    4207        1531 :                 .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
    4208        1531 :                 .offset = dp->tdir_offset.toff_long8;
    4209        1531 :             if (tif->tif_flags & TIFF_SWAB)
    4210          30 :                 TIFFSwabLong8(
    4211             :                     &tif->tif_dir
    4212          30 :                          .td_dirdatasize_offsets[tif->tif_dir
    4213          30 :                                                      .td_dirdatasize_Noffsets]
    4214             :                          .offset);
    4215             :         }
    4216             :         tif->tif_dir
    4217      198241 :             .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
    4218      198241 :             .length = datalength;
    4219      198241 :         tif->tif_dir.td_dirdatasize_Noffsets++;
    4220             :     }
    4221      362555 :     return true;
    4222             : }
    4223             : 
    4224             : /*
    4225             :  * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
    4226             :  */
    4227       73502 : static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
    4228             : {
    4229       73502 :     const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a;
    4230       73502 :     const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b;
    4231             :     /* Compare offsets */
    4232       73502 :     if (ta->offset > tb->offset)
    4233        9765 :         return 1;
    4234       63737 :     else if (ta->offset < tb->offset)
    4235       63737 :         return -1;
    4236             :     else
    4237           0 :         return 0;
    4238             : }
    4239             : 
    4240             : /*
    4241             :  * Determine the IFD data size after reading an IFD from the file that can be
    4242             :  * overwritten and saving it in tif_dir.td_dirdatasize_read. This data size
    4243             :  * includes the IFD entries themselves as well as the data that does not fit
    4244             :  * directly into the IFD entries but is located directly after the IFD entries
    4245             :  * in the file.
    4246             :  */
    4247       54074 : static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount)
    4248             : {
    4249             :     /* IFD data size is only needed if file-writing is enabled.
    4250             :      * This also avoids the seek() to EOF to determine the file size, which
    4251             :      * causes the stdin-streaming-friendly mode of libtiff for GDAL to fail. */
    4252       54074 :     if (tif->tif_mode == O_RDONLY)
    4253       35318 :         return;
    4254             : 
    4255             :     /* Sort TIFFEntryOffsetAndLength array in ascending order. */
    4256       18756 :     qsort(tif->tif_dir.td_dirdatasize_offsets,
    4257       18756 :           tif->tif_dir.td_dirdatasize_Noffsets,
    4258             :           sizeof(TIFFEntryOffsetAndLength), cmpTIFFEntryOffsetAndLength);
    4259             : 
    4260             :     /* Get offset of end of IFD entry space. */
    4261             :     uint64_t IFDendoffset;
    4262       18745 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    4263       18284 :         IFDendoffset = tif->tif_diroff + 2 + dircount * 12 + 4;
    4264             :     else
    4265         461 :         IFDendoffset = tif->tif_diroff + 8 + dircount * 20 + 8;
    4266             : 
    4267             :     /* Check which offsets are right behind IFD entries. However, LibTIFF
    4268             :      * increments the writing address for every external data to an even offset.
    4269             :      * Thus gaps of 1 byte can occur. */
    4270       18745 :     uint64_t size = 0;
    4271             :     uint64_t offset;
    4272             :     uint32_t i;
    4273       81023 :     for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
    4274             :     {
    4275       62314 :         offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
    4276       62314 :         if (offset == IFDendoffset)
    4277             :         {
    4278       59857 :             size += tif->tif_dir.td_dirdatasize_offsets[i].length;
    4279       59857 :             IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
    4280             :         }
    4281        2457 :         else if (offset == IFDendoffset + 1)
    4282             :         {
    4283             :             /* Add gap byte after previous IFD data set. */
    4284        2421 :             size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
    4285        2421 :             IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
    4286             :         }
    4287             :         else
    4288             :         {
    4289             :             /* Further data is no more continuously after IFD */
    4290          36 :             break;
    4291             :         }
    4292             :     }
    4293             :     /* Check for gap byte of some easy cases. This should cover 90% of cases.
    4294             :      * Otherwise, IFD will be re-written even it might be safely overwritten. */
    4295       18745 :     if (tif->tif_nextdiroff != 0)
    4296             :     {
    4297        2774 :         if (tif->tif_nextdiroff == IFDendoffset + 1)
    4298         759 :             size++;
    4299             :     }
    4300             :     else
    4301             :     {
    4302             :         /* Check for IFD data ends at EOF. Then IFD can always be safely
    4303             :          * overwritten. */
    4304       15971 :         offset = TIFFSeekFile(tif, 0, SEEK_END);
    4305       15971 :         if (offset == IFDendoffset)
    4306             :         {
    4307       14517 :             tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
    4308       14517 :             return;
    4309             :         }
    4310             :     }
    4311             : 
    4312             :     /* Finally, add the size of the IFD tag entries themselves. */
    4313        4228 :     if (!(tif->tif_flags & TIFF_BIGTIFF))
    4314        4033 :         tif->tif_dir.td_dirdatasize_read = 2 + dircount * 12 + 4 + size;
    4315             :     else
    4316         195 :         tif->tif_dir.td_dirdatasize_read = 8 + dircount * 20 + 8 + size;
    4317             : } /*-- CalcFinalIFDdatasizeReading() --*/
    4318             : 
    4319             : /*
    4320             :  * Read the next TIFF directory from a file and convert it to the internal
    4321             :  * format. We read directories sequentially.
    4322             :  */
    4323       62601 : int TIFFReadDirectory(TIFF *tif)
    4324             : {
    4325             :     static const char module[] = "TIFFReadDirectory";
    4326             :     TIFFDirEntry *dir;
    4327             :     uint16_t dircount;
    4328             :     TIFFDirEntry *dp;
    4329             :     uint16_t di;
    4330             :     const TIFFField *fip;
    4331       62601 :     uint32_t fii = FAILED_FII;
    4332             :     toff_t nextdiroff;
    4333       62601 :     int bitspersample_read = FALSE;
    4334             :     int color_channels;
    4335             : 
    4336       62601 :     if (tif->tif_nextdiroff == 0)
    4337             :     {
    4338             :         /* In this special case, tif_diroff needs also to be set to 0.
    4339             :          * This is behind the last IFD, thus no checking or reading necessary.
    4340             :          */
    4341        8506 :         tif->tif_diroff = tif->tif_nextdiroff;
    4342        8506 :         return 0;
    4343             :     }
    4344             : 
    4345       54095 :     nextdiroff = tif->tif_nextdiroff;
    4346             :     /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
    4347             :      * reading of the directory. Otherwise, invalid IFD offsets could corrupt
    4348             :      * the IFD list. */
    4349       54095 :     if (!_TIFFCheckDirNumberAndOffset(tif,
    4350       54095 :                                       tif->tif_curdir ==
    4351             :                                               TIFF_NON_EXISTENT_DIR_NUMBER
    4352             :                                           ? 0
    4353        3694 :                                           : tif->tif_curdir + 1,
    4354             :                                       nextdiroff))
    4355             :     {
    4356           0 :         return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
    4357             :                      IFDs) */
    4358             :     }
    4359       54067 :     dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
    4360       54020 :     if (!dircount)
    4361             :     {
    4362          12 :         TIFFErrorExtR(tif, module,
    4363             :                       "Failed to read directory at offset %" PRIu64,
    4364             :                       nextdiroff);
    4365          17 :         return 0;
    4366             :     }
    4367             :     /* Set global values after a valid directory has been fetched.
    4368             :      * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
    4369             :      * beginning. */
    4370       54008 :     if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
    4371       50316 :         tif->tif_curdir = 0;
    4372             :     else
    4373        3692 :         tif->tif_curdir++;
    4374             : 
    4375       54008 :     TIFFReadDirectoryCheckOrder(tif, dir, dircount);
    4376             : 
    4377             :     /*
    4378             :      * Mark duplicates of any tag to be ignored (bugzilla 1994)
    4379             :      * to avoid certain pathological problems.
    4380             :      */
    4381             :     {
    4382             :         TIFFDirEntry *ma;
    4383             :         uint16_t mb;
    4384      818157 :         for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
    4385             :         {
    4386             :             TIFFDirEntry *na;
    4387             :             uint16_t nb;
    4388     5940690 :             for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
    4389             :             {
    4390     5176610 :                 if (ma->tdir_tag == na->tdir_tag)
    4391             :                 {
    4392       12237 :                     na->tdir_ignore = TRUE;
    4393             :                 }
    4394             :             }
    4395             :         }
    4396             :     }
    4397             : 
    4398       54076 :     tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
    4399       54076 :     tif->tif_flags &= ~TIFF_BUF4WRITE;   /* reset before new dir */
    4400       54076 :     tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
    4401             : 
    4402             :     /* When changing directory, in deferred strile loading mode, we must also
    4403             :      * unset the TIFF_LAZYSTRILELOAD_DONE bit if it was initially set,
    4404             :      * to make sure the strile offset/bytecount are read again (when they fit
    4405             :      * in the tag data area).
    4406             :      */
    4407       54076 :     tif->tif_flags &= ~TIFF_LAZYSTRILELOAD_DONE;
    4408             : 
    4409             :     /* free any old stuff and reinit */
    4410       54076 :     TIFFFreeDirectory(tif);
    4411       54024 :     TIFFDefaultDirectory(tif);
    4412             : 
    4413             :     /* After setup a fresh directory indicate that now active IFD is also
    4414             :      * present on file, even if its entries could not be read successfully
    4415             :      * below.  */
    4416       54014 :     tif->tif_dir.td_iswrittentofile = TRUE;
    4417             : 
    4418             :     /* Allocate arrays for offset values outside IFD entry for IFD data size
    4419             :      * checking. Note: Counter are reset within TIFFFreeDirectory(). */
    4420       54018 :     tif->tif_dir.td_dirdatasize_offsets =
    4421       54014 :         (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
    4422       54014 :             tif, dircount * sizeof(TIFFEntryOffsetAndLength));
    4423       54018 :     if (tif->tif_dir.td_dirdatasize_offsets == NULL)
    4424             :     {
    4425           0 :         TIFFErrorExtR(
    4426             :             tif, module,
    4427             :             "Failed to allocate memory for counting IFD data size at reading");
    4428           0 :         goto bad;
    4429             :     }
    4430             :     /*
    4431             :      * Electronic Arts writes gray-scale TIFF files
    4432             :      * without a PlanarConfiguration directory entry.
    4433             :      * Thus we setup a default value here, even though
    4434             :      * the TIFF spec says there is no default value.
    4435             :      * After PlanarConfiguration is preset in TIFFDefaultDirectory()
    4436             :      * the following setting is not needed, but does not harm either.
    4437             :      */
    4438       54018 :     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    4439             :     /*
    4440             :      * Setup default value and then make a pass over
    4441             :      * the fields to check type and tag information,
    4442             :      * and to extract info required to size data
    4443             :      * structures.  A second pass is made afterwards
    4444             :      * to read in everything not taken in the first pass.
    4445             :      * But we must process the Compression tag first
    4446             :      * in order to merge in codec-private tag definitions (otherwise
    4447             :      * we may get complaints about unknown tags).  However, the
    4448             :      * Compression tag may be dependent on the SamplesPerPixel
    4449             :      * tag value because older TIFF specs permitted Compression
    4450             :      * to be written as a SamplesPerPixel-count tag entry.
    4451             :      * Thus if we don't first figure out the correct SamplesPerPixel
    4452             :      * tag value then we may end up ignoring the Compression tag
    4453             :      * value because it has an incorrect count value (if the
    4454             :      * true value of SamplesPerPixel is not 1).
    4455             :      */
    4456             :     dp =
    4457       54057 :         TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
    4458       54046 :     if (dp)
    4459             :     {
    4460       54016 :         if (!TIFFFetchNormalTag(tif, dp, 0))
    4461           0 :             goto bad;
    4462       54017 :         dp->tdir_ignore = TRUE;
    4463             :     }
    4464       54047 :     dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
    4465       54041 :     if (dp)
    4466             :     {
    4467             :         /*
    4468             :          * The 5.0 spec says the Compression tag has one value, while
    4469             :          * earlier specs say it has one value per sample.  Because of
    4470             :          * this, we accept the tag if one value is supplied with either
    4471             :          * count.
    4472             :          */
    4473             :         uint16_t value;
    4474             :         enum TIFFReadDirEntryErr err;
    4475       54028 :         err = TIFFReadDirEntryShort(tif, dp, &value);
    4476       54039 :         if (err == TIFFReadDirEntryErrCount)
    4477           0 :             err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
    4478       54014 :         if (err != TIFFReadDirEntryErrOk)
    4479             :         {
    4480           0 :             TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
    4481           0 :             goto bad;
    4482             :         }
    4483       54014 :         if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
    4484           0 :             goto bad;
    4485       54053 :         dp->tdir_ignore = TRUE;
    4486             :     }
    4487             :     else
    4488             :     {
    4489          13 :         if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
    4490           0 :             goto bad;
    4491             :     }
    4492             :     /*
    4493             :      * First real pass over the directory.
    4494             :      */
    4495      818347 :     for (di = 0, dp = dir; di < dircount; di++, dp++)
    4496             :     {
    4497      764108 :         if (!dp->tdir_ignore)
    4498             :         {
    4499      655448 :             TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    4500      655805 :             if (fii == FAILED_FII)
    4501             :             {
    4502         437 :                 if (tif->tif_warn_about_unknown_tags)
    4503             :                 {
    4504           0 :                     TIFFWarningExtR(tif, module,
    4505             :                                     "Unknown field with tag %" PRIu16
    4506             :                                     " (0x%" PRIx16 ") encountered",
    4507           0 :                                     dp->tdir_tag, dp->tdir_tag);
    4508             :                 }
    4509             :                 /* the following knowingly leaks the
    4510             :                    anonymous field structure */
    4511         437 :                 const TIFFField *fld = _TIFFCreateAnonField(
    4512         437 :                     tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
    4513         437 :                 if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
    4514             :                 {
    4515           0 :                     TIFFWarningExtR(
    4516             :                         tif, module,
    4517             :                         "Registering anonymous field with tag %" PRIu16
    4518             :                         " (0x%" PRIx16 ") failed",
    4519           0 :                         dp->tdir_tag, dp->tdir_tag);
    4520           0 :                     dp->tdir_ignore = TRUE;
    4521             :                 }
    4522             :                 else
    4523             :                 {
    4524         437 :                     TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    4525         437 :                     assert(fii != FAILED_FII);
    4526             :                 }
    4527             :             }
    4528             :         }
    4529      764425 :         if (!dp->tdir_ignore)
    4530             :         {
    4531      655722 :             fip = tif->tif_fields[fii];
    4532      655722 :             if (fip->field_bit == FIELD_IGNORE)
    4533           0 :                 dp->tdir_ignore = TRUE;
    4534             :             else
    4535             :             {
    4536      655722 :                 switch (dp->tdir_tag)
    4537             :                 {
    4538      108133 :                     case TIFFTAG_STRIPOFFSETS:
    4539             :                     case TIFFTAG_STRIPBYTECOUNTS:
    4540             :                     case TIFFTAG_TILEOFFSETS:
    4541             :                     case TIFFTAG_TILEBYTECOUNTS:
    4542      108133 :                         TIFFSetFieldBit(tif, fip->field_bit);
    4543      108133 :                         break;
    4544      231140 :                     case TIFFTAG_IMAGEWIDTH:
    4545             :                     case TIFFTAG_IMAGELENGTH:
    4546             :                     case TIFFTAG_IMAGEDEPTH:
    4547             :                     case TIFFTAG_TILELENGTH:
    4548             :                     case TIFFTAG_TILEWIDTH:
    4549             :                     case TIFFTAG_TILEDEPTH:
    4550             :                     case TIFFTAG_PLANARCONFIG:
    4551             :                     case TIFFTAG_ROWSPERSTRIP:
    4552             :                     case TIFFTAG_EXTRASAMPLES:
    4553      231140 :                         if (!TIFFFetchNormalTag(tif, dp, 0))
    4554           0 :                             goto bad;
    4555      231079 :                         dp->tdir_ignore = TRUE;
    4556      231079 :                         break;
    4557      316449 :                     default:
    4558      316449 :                         if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
    4559           9 :                             dp->tdir_ignore = TRUE;
    4560      316368 :                         break;
    4561             :                 }
    4562      108703 :             }
    4563             :         }
    4564             :     }
    4565             :     /*
    4566             :      * XXX: OJPEG hack.
    4567             :      * If a) compression is OJPEG, b) planarconfig tag says it's separate,
    4568             :      * c) strip offsets/bytecounts tag are both present and
    4569             :      * d) both contain exactly one value, then we consistently find
    4570             :      * that the buggy implementation of the buggy compression scheme
    4571             :      * matches contig planarconfig best. So we 'fix-up' the tag here
    4572             :      */
    4573       54239 :     if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
    4574           2 :         (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
    4575             :     {
    4576           0 :         if (!_TIFFFillStriles(tif))
    4577           0 :             goto bad;
    4578           0 :         dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
    4579             :                                         TIFFTAG_STRIPOFFSETS);
    4580           0 :         if ((dp != 0) && (dp->tdir_count == 1))
    4581             :         {
    4582           0 :             dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
    4583             :                                             TIFFTAG_STRIPBYTECOUNTS);
    4584           0 :             if ((dp != 0) && (dp->tdir_count == 1))
    4585             :             {
    4586           0 :                 tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
    4587           0 :                 TIFFWarningExtR(tif, module,
    4588             :                                 "Planarconfig tag value assumed incorrect, "
    4589             :                                 "assuming data is contig instead of chunky");
    4590             :             }
    4591             :         }
    4592             :     }
    4593             :     /*
    4594             :      * Allocate directory structure and setup defaults.
    4595             :      */
    4596       54061 :     if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
    4597             :     {
    4598           0 :         MissingRequired(tif, "ImageLength");
    4599           0 :         goto bad;
    4600             :     }
    4601             : 
    4602             :     /*
    4603             :      * Second pass: extract other information.
    4604             :      */
    4605      818059 :     for (di = 0, dp = dir; di < dircount; di++, dp++)
    4606             :     {
    4607      763978 :         if (!dp->tdir_ignore)
    4608             :         {
    4609      424303 :             switch (dp->tdir_tag)
    4610             :             {
    4611      107308 :                 case TIFFTAG_MINSAMPLEVALUE:
    4612             :                 case TIFFTAG_MAXSAMPLEVALUE:
    4613             :                 case TIFFTAG_BITSPERSAMPLE:
    4614             :                 case TIFFTAG_DATATYPE:
    4615             :                 case TIFFTAG_SAMPLEFORMAT:
    4616             :                     /*
    4617             :                      * The MinSampleValue, MaxSampleValue, BitsPerSample
    4618             :                      * DataType and SampleFormat tags are supposed to be
    4619             :                      * written as one value/sample, but some vendors
    4620             :                      * incorrectly write one value only -- so we accept
    4621             :                      * that as well (yuck). Other vendors write correct
    4622             :                      * value for NumberOfSamples, but incorrect one for
    4623             :                      * BitsPerSample and friends, and we will read this
    4624             :                      * too.
    4625             :                      */
    4626             :                     {
    4627             :                         uint16_t value;
    4628             :                         enum TIFFReadDirEntryErr err;
    4629      107308 :                         err = TIFFReadDirEntryShort(tif, dp, &value);
    4630      107300 :                         if (!EvaluateIFDdatasizeReading(tif, dp))
    4631           0 :                             goto bad;
    4632      107266 :                         if (err == TIFFReadDirEntryErrCount)
    4633             :                             err =
    4634       32704 :                                 TIFFReadDirEntryPersampleShort(tif, dp, &value);
    4635      107262 :                         if (err != TIFFReadDirEntryErrOk)
    4636             :                         {
    4637           0 :                             fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4638           0 :                             TIFFReadDirEntryOutputErr(
    4639             :                                 tif, err, module,
    4640             :                                 fip ? fip->field_name : "unknown tagname", 0);
    4641           0 :                             goto bad;
    4642             :                         }
    4643      107262 :                         if (!TIFFSetField(tif, dp->tdir_tag, value))
    4644           0 :                             goto bad;
    4645      107314 :                         if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
    4646       54021 :                             bitspersample_read = TRUE;
    4647             :                     }
    4648      107314 :                     break;
    4649           0 :                 case TIFFTAG_SMINSAMPLEVALUE:
    4650             :                 case TIFFTAG_SMAXSAMPLEVALUE:
    4651             :                 {
    4652             : 
    4653           0 :                     double *data = NULL;
    4654             :                     enum TIFFReadDirEntryErr err;
    4655             :                     uint32_t saved_flags;
    4656             :                     int m;
    4657           0 :                     if (dp->tdir_count !=
    4658           0 :                         (uint64_t)tif->tif_dir.td_samplesperpixel)
    4659           0 :                         err = TIFFReadDirEntryErrCount;
    4660             :                     else
    4661           0 :                         err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    4662           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4663           0 :                         goto bad;
    4664           0 :                     if (err != TIFFReadDirEntryErrOk)
    4665             :                     {
    4666           0 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4667           0 :                         TIFFReadDirEntryOutputErr(
    4668             :                             tif, err, module,
    4669             :                             fip ? fip->field_name : "unknown tagname", 0);
    4670           0 :                         goto bad;
    4671             :                     }
    4672           0 :                     saved_flags = tif->tif_flags;
    4673           0 :                     tif->tif_flags |= TIFF_PERSAMPLE;
    4674           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    4675           0 :                     tif->tif_flags = saved_flags;
    4676           0 :                     _TIFFfreeExt(tif, data);
    4677           0 :                     if (!m)
    4678           0 :                         goto bad;
    4679             :                 }
    4680           0 :                 break;
    4681       54051 :                 case TIFFTAG_STRIPOFFSETS:
    4682             :                 case TIFFTAG_TILEOFFSETS:
    4683             :                 {
    4684       54051 :                     switch (dp->tdir_type)
    4685             :                     {
    4686       52650 :                         case TIFF_SHORT:
    4687             :                         case TIFF_LONG:
    4688             :                         case TIFF_LONG8:
    4689       52650 :                             break;
    4690        1401 :                         default:
    4691             :                             /* Warn except if directory typically created with
    4692             :                              * TIFFDeferStrileArrayWriting() */
    4693        1401 :                             if (!(tif->tif_mode == O_RDWR &&
    4694        1369 :                                   dp->tdir_count == 0 && dp->tdir_type == 0 &&
    4695        1369 :                                   dp->tdir_offset.toff_long8 == 0))
    4696             :                             {
    4697          32 :                                 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4698          11 :                                 TIFFWarningExtR(
    4699             :                                     tif, module, "Invalid data type for tag %s",
    4700             :                                     fip ? fip->field_name : "unknown tagname");
    4701             :                             }
    4702        1380 :                             break;
    4703             :                     }
    4704       54030 :                     _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
    4705             :                                 sizeof(TIFFDirEntry));
    4706       54007 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4707           1 :                         goto bad;
    4708             :                 }
    4709       53999 :                 break;
    4710       54022 :                 case TIFFTAG_STRIPBYTECOUNTS:
    4711             :                 case TIFFTAG_TILEBYTECOUNTS:
    4712             :                 {
    4713       54022 :                     switch (dp->tdir_type)
    4714             :                     {
    4715       52661 :                         case TIFF_SHORT:
    4716             :                         case TIFF_LONG:
    4717             :                         case TIFF_LONG8:
    4718       52661 :                             break;
    4719        1361 :                         default:
    4720             :                             /* Warn except if directory typically created with
    4721             :                              * TIFFDeferStrileArrayWriting() */
    4722        1361 :                             if (!(tif->tif_mode == O_RDWR &&
    4723        1369 :                                   dp->tdir_count == 0 && dp->tdir_type == 0 &&
    4724        1369 :                                   dp->tdir_offset.toff_long8 == 0))
    4725             :                             {
    4726           0 :                                 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4727           9 :                                 TIFFWarningExtR(
    4728             :                                     tif, module, "Invalid data type for tag %s",
    4729             :                                     fip ? fip->field_name : "unknown tagname");
    4730             :                             }
    4731        1378 :                             break;
    4732             :                     }
    4733       54039 :                     _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
    4734             :                                 sizeof(TIFFDirEntry));
    4735       53995 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4736           0 :                         goto bad;
    4737             :                 }
    4738       54027 :                 break;
    4739         376 :                 case TIFFTAG_COLORMAP:
    4740             :                 case TIFFTAG_TRANSFERFUNCTION:
    4741             :                 {
    4742             :                     enum TIFFReadDirEntryErr err;
    4743             :                     uint32_t countpersample;
    4744             :                     uint32_t countrequired;
    4745             :                     uint32_t incrementpersample;
    4746         376 :                     uint16_t *value = NULL;
    4747             :                     /* It would be dangerous to instantiate those tag values */
    4748             :                     /* since if td_bitspersample has not yet been read (due to
    4749             :                      */
    4750             :                     /* unordered tags), it could be read afterwards with a */
    4751             :                     /* values greater than the default one (1), which may cause
    4752             :                      */
    4753             :                     /* crashes in user code */
    4754         376 :                     if (!bitspersample_read)
    4755             :                     {
    4756           0 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4757           0 :                         TIFFWarningExtR(
    4758             :                             tif, module,
    4759             :                             "Ignoring %s since BitsPerSample tag not found",
    4760             :                             fip ? fip->field_name : "unknown tagname");
    4761           0 :                         continue;
    4762             :                     }
    4763             :                     /* ColorMap or TransferFunction for high bit */
    4764             :                     /* depths do not make much sense and could be */
    4765             :                     /* used as a denial of service vector */
    4766         376 :                     if (tif->tif_dir.td_bitspersample > 24)
    4767             :                     {
    4768           0 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4769           0 :                         TIFFWarningExtR(
    4770             :                             tif, module,
    4771             :                             "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
    4772             :                             fip ? fip->field_name : "unknown tagname",
    4773           0 :                             tif->tif_dir.td_bitspersample);
    4774           0 :                         continue;
    4775             :                     }
    4776         376 :                     countpersample = (1U << tif->tif_dir.td_bitspersample);
    4777         376 :                     if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
    4778          15 :                         (dp->tdir_count == (uint64_t)countpersample))
    4779             :                     {
    4780           0 :                         countrequired = countpersample;
    4781           0 :                         incrementpersample = 0;
    4782             :                     }
    4783             :                     else
    4784             :                     {
    4785         376 :                         countrequired = 3 * countpersample;
    4786         376 :                         incrementpersample = countpersample;
    4787             :                     }
    4788         376 :                     if (dp->tdir_count != (uint64_t)countrequired)
    4789           0 :                         err = TIFFReadDirEntryErrCount;
    4790             :                     else
    4791         376 :                         err = TIFFReadDirEntryShortArray(tif, dp, &value);
    4792         376 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    4793           0 :                         goto bad;
    4794         376 :                     if (err != TIFFReadDirEntryErrOk)
    4795             :                     {
    4796           1 :                         fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4797           1 :                         TIFFReadDirEntryOutputErr(
    4798             :                             tif, err, module,
    4799             :                             fip ? fip->field_name : "unknown tagname", 1);
    4800             :                     }
    4801             :                     else
    4802             :                     {
    4803         375 :                         TIFFSetField(tif, dp->tdir_tag, value,
    4804         375 :                                      value + incrementpersample,
    4805         375 :                                      value + 2 * incrementpersample);
    4806         375 :                         _TIFFfreeExt(tif, value);
    4807             :                     }
    4808             :                 }
    4809         376 :                 break;
    4810             :                     /* BEGIN REV 4.0 COMPATIBILITY */
    4811           0 :                 case TIFFTAG_OSUBFILETYPE:
    4812             :                 {
    4813             :                     uint16_t valueo;
    4814             :                     uint32_t value;
    4815           0 :                     if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
    4816             :                         TIFFReadDirEntryErrOk)
    4817             :                     {
    4818           0 :                         switch (valueo)
    4819             :                         {
    4820           0 :                             case OFILETYPE_REDUCEDIMAGE:
    4821           0 :                                 value = FILETYPE_REDUCEDIMAGE;
    4822           0 :                                 break;
    4823           0 :                             case OFILETYPE_PAGE:
    4824           0 :                                 value = FILETYPE_PAGE;
    4825           0 :                                 break;
    4826           0 :                             default:
    4827           0 :                                 value = 0;
    4828           0 :                                 break;
    4829             :                         }
    4830           0 :                         if (value != 0)
    4831           0 :                             TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
    4832             :                     }
    4833             :                 }
    4834           0 :                 break;
    4835             :                 /* END REV 4.0 COMPATIBILITY */
    4836             : #if 0
    4837             :                 case TIFFTAG_EP_BATTERYLEVEL:
    4838             :                     /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
    4839             :                      * LibTiff defines it as ASCII and converts RATIONAL to an
    4840             :                      * ASCII string. */
    4841             :                     switch (dp->tdir_type)
    4842             :                     {
    4843             :                         case TIFF_RATIONAL:
    4844             :                         {
    4845             :                             /* Read rational and convert to ASCII*/
    4846             :                             enum TIFFReadDirEntryErr err;
    4847             :                             TIFFRational_t rValue;
    4848             :                             err = TIFFReadDirEntryCheckedRationalDirect(
    4849             :                                 tif, dp, &rValue);
    4850             :                             if (err != TIFFReadDirEntryErrOk)
    4851             :                             {
    4852             :                                 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4853             :                                 TIFFReadDirEntryOutputErr(
    4854             :                                     tif, err, module,
    4855             :                                     fip ? fip->field_name : "unknown tagname",
    4856             :                                     1);
    4857             :                             }
    4858             :                             else
    4859             :                             {
    4860             :                                 char szAux[32];
    4861             :                                 snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
    4862             :                                          rValue.uNum, rValue.uDenom);
    4863             :                                 TIFFSetField(tif, dp->tdir_tag, szAux);
    4864             :                             }
    4865             :                         }
    4866             :                         break;
    4867             :                         case TIFF_ASCII:
    4868             :                             (void)TIFFFetchNormalTag(tif, dp, TRUE);
    4869             :                             break;
    4870             :                         default:
    4871             :                             fip = TIFFFieldWithTag(tif, dp->tdir_tag);
    4872             :                             TIFFWarningExtR(tif, module,
    4873             :                                             "Invalid data type for tag %s. "
    4874             :                                             "ASCII or RATIONAL expected",
    4875             :                                             fip ? fip->field_name
    4876             :                                                 : "unknown tagname");
    4877             :                             break;
    4878             :                     }
    4879             :                     break;
    4880             : #endif
    4881      208546 :                 default:
    4882      208546 :                     (void)TIFFFetchNormalTag(tif, dp, TRUE);
    4883      208607 :                     break;
    4884             :             } /* -- switch (dp->tdir_tag) -- */
    4885      339675 :         }     /* -- if (!dp->tdir_ignore) */
    4886             :     }         /* -- for-loop -- */
    4887             : 
    4888             :     /* Evaluate final IFD data size. */
    4889       54081 :     CalcFinalIFDdatasizeReading(tif, dircount);
    4890             : 
    4891             :     /*
    4892             :      * OJPEG hack:
    4893             :      * - If a) compression is OJPEG, and b) photometric tag is missing,
    4894             :      * then we consistently find that photometric should be YCbCr
    4895             :      * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
    4896             :      * then we consistently find that the buggy implementation of the
    4897             :      * buggy compression scheme matches photometric YCbCr instead.
    4898             :      * - If a) compression is OJPEG, and b) bitspersample tag is missing,
    4899             :      * then we consistently find bitspersample should be 8.
    4900             :      * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
    4901             :      * and c) photometric is RGB or YCbCr, then we consistently find
    4902             :      * samplesperpixel should be 3
    4903             :      * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
    4904             :      * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
    4905             :      * find samplesperpixel should be 3
    4906             :      */
    4907       53990 :     if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
    4908             :     {
    4909           2 :         if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
    4910             :         {
    4911           0 :             TIFFWarningExtR(
    4912             :                 tif, module,
    4913             :                 "Photometric tag is missing, assuming data is YCbCr");
    4914           0 :             if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
    4915           0 :                 goto bad;
    4916             :         }
    4917           2 :         else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
    4918             :         {
    4919           0 :             tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
    4920           0 :             TIFFWarningExtR(tif, module,
    4921             :                             "Photometric tag value assumed incorrect, "
    4922             :                             "assuming data is YCbCr instead of RGB");
    4923             :         }
    4924           2 :         if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
    4925             :         {
    4926           0 :             TIFFWarningExtR(
    4927             :                 tif, module,
    4928             :                 "BitsPerSample tag is missing, assuming 8 bits per sample");
    4929           0 :             if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
    4930           0 :                 goto bad;
    4931             :         }
    4932           2 :         if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
    4933             :         {
    4934           0 :             if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
    4935             :             {
    4936           0 :                 TIFFWarningExtR(tif, module,
    4937             :                                 "SamplesPerPixel tag is missing, "
    4938             :                                 "assuming correct SamplesPerPixel value is 3");
    4939           0 :                 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
    4940           0 :                     goto bad;
    4941             :             }
    4942           0 :             if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
    4943             :             {
    4944           0 :                 TIFFWarningExtR(tif, module,
    4945             :                                 "SamplesPerPixel tag is missing, "
    4946             :                                 "applying correct SamplesPerPixel value of 3");
    4947           0 :                 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
    4948           0 :                     goto bad;
    4949             :             }
    4950           0 :             else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
    4951           0 :                      (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
    4952             :             {
    4953             :                 /*
    4954             :                  * SamplesPerPixel tag is missing, but is not required
    4955             :                  * by spec.  Assume correct SamplesPerPixel value of 1.
    4956             :                  */
    4957           0 :                 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
    4958           0 :                     goto bad;
    4959             :             }
    4960             :         }
    4961             :     }
    4962             : 
    4963             :     /*
    4964             :      * Setup appropriate structures (by strip or by tile)
    4965             :      * We do that only after the above OJPEG hack which alters SamplesPerPixel
    4966             :      * and thus influences the number of strips in the separate planarconfig.
    4967             :      */
    4968       54030 :     if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
    4969             :     {
    4970       44753 :         tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
    4971       44704 :         tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
    4972       44704 :         tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
    4973       44704 :         tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
    4974       44704 :         tif->tif_flags &= ~TIFF_ISTILED;
    4975             :     }
    4976             :     else
    4977             :     {
    4978        9277 :         tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
    4979        9276 :         tif->tif_flags |= TIFF_ISTILED;
    4980             :     }
    4981       53980 :     if (!tif->tif_dir.td_nstrips)
    4982             :     {
    4983           0 :         TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
    4984           0 :                       isTiled(tif) ? "tiles" : "strips");
    4985           0 :         goto bad;
    4986             :     }
    4987       53980 :     tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
    4988       53980 :     if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
    4989        9378 :         tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
    4990       53980 :     if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
    4991             :     {
    4992             : #ifdef OJPEG_SUPPORT
    4993           0 :         if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
    4994           0 :             (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
    4995             :         {
    4996             :             /*
    4997             :              * XXX: OJPEG hack.
    4998             :              * If a) compression is OJPEG, b) it's not a tiled TIFF,
    4999             :              * and c) the number of strips is 1,
    5000             :              * then we tolerate the absence of stripoffsets tag,
    5001             :              * because, presumably, all required data is in the
    5002             :              * JpegInterchangeFormat stream.
    5003             :              */
    5004           0 :             TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
    5005             :         }
    5006             :         else
    5007             : #endif
    5008             :         {
    5009           0 :             MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
    5010           0 :             goto bad;
    5011             :         }
    5012             :     }
    5013             : 
    5014       53980 :     if (tif->tif_mode == O_RDWR &&
    5015       18745 :         tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
    5016       18745 :         tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
    5017        1369 :         tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
    5018        1369 :         tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
    5019        1369 :         tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
    5020        1369 :         tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
    5021        1369 :         tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
    5022        1369 :         tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
    5023             :     {
    5024             :         /* Directory typically created with TIFFDeferStrileArrayWriting() */
    5025        1369 :         TIFFSetupStrips(tif);
    5026             :     }
    5027       52611 :     else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
    5028             :     {
    5029       17632 :         if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
    5030             :         {
    5031       17631 :             if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
    5032             :                                      tif->tif_dir.td_nstrips,
    5033             :                                      &tif->tif_dir.td_stripoffset_p))
    5034             :             {
    5035           0 :                 goto bad;
    5036             :             }
    5037             :         }
    5038       17632 :         if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
    5039             :         {
    5040       17632 :             if (!TIFFFetchStripThing(
    5041             :                     tif, &(tif->tif_dir.td_stripbytecount_entry),
    5042             :                     tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
    5043             :             {
    5044           0 :                 goto bad;
    5045             :             }
    5046             :         }
    5047             :     }
    5048             : 
    5049             :     /*
    5050             :      * Make sure all non-color channels are extrasamples.
    5051             :      * If it's not the case, define them as such.
    5052             :      */
    5053       53981 :     color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
    5054       53972 :     if (color_channels &&
    5055       53988 :         tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
    5056             :             color_channels)
    5057             :     {
    5058             :         uint16_t old_extrasamples;
    5059             :         uint16_t *new_sampleinfo;
    5060             : 
    5061           3 :         TIFFWarningExtR(
    5062             :             tif, module,
    5063             :             "Sum of Photometric type-related "
    5064             :             "color channels and ExtraSamples doesn't match SamplesPerPixel. "
    5065             :             "Defining non-color channels as ExtraSamples.");
    5066             : 
    5067           3 :         old_extrasamples = tif->tif_dir.td_extrasamples;
    5068           3 :         tif->tif_dir.td_extrasamples =
    5069           3 :             (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
    5070             : 
    5071             :         // sampleinfo should contain information relative to these new extra
    5072             :         // samples
    5073           3 :         new_sampleinfo = (uint16_t *)_TIFFcallocExt(
    5074           3 :             tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
    5075           3 :         if (!new_sampleinfo)
    5076             :         {
    5077           0 :             TIFFErrorExtR(tif, module,
    5078             :                           "Failed to allocate memory for "
    5079             :                           "temporary new sampleinfo array "
    5080             :                           "(%" PRIu16 " 16 bit elements)",
    5081           0 :                           tif->tif_dir.td_extrasamples);
    5082           0 :             goto bad;
    5083             :         }
    5084             : 
    5085           3 :         if (old_extrasamples > 0)
    5086           0 :             memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
    5087             :                    old_extrasamples * sizeof(uint16_t));
    5088           3 :         _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
    5089           3 :                               tif->tif_dir.td_extrasamples);
    5090           3 :         _TIFFfreeExt(tif, new_sampleinfo);
    5091             :     }
    5092             : 
    5093             :     /*
    5094             :      * Verify Palette image has a Colormap.
    5095             :      */
    5096       53969 :     if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
    5097         357 :         !TIFFFieldSet(tif, FIELD_COLORMAP))
    5098             :     {
    5099           1 :         if (tif->tif_dir.td_bitspersample >= 8 &&
    5100           1 :             tif->tif_dir.td_samplesperpixel == 3)
    5101           0 :             tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
    5102           1 :         else if (tif->tif_dir.td_bitspersample >= 8)
    5103           1 :             tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
    5104             :         else
    5105             :         {
    5106           0 :             MissingRequired(tif, "Colormap");
    5107           0 :             goto bad;
    5108             :         }
    5109             :     }
    5110             :     /*
    5111             :      * OJPEG hack:
    5112             :      * We do no further messing with strip/tile offsets/bytecounts in OJPEG
    5113             :      * TIFFs
    5114             :      */
    5115       53969 :     if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
    5116             :     {
    5117             :         /*
    5118             :          * Attempt to deal with a missing StripByteCounts tag.
    5119             :          */
    5120       53978 :         if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
    5121             :         {
    5122             :             /*
    5123             :              * Some manufacturers violate the spec by not giving
    5124             :              * the size of the strips.  In this case, assume there
    5125             :              * is one uncompressed strip of data.
    5126             :              */
    5127           5 :             if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    5128           5 :                  tif->tif_dir.td_nstrips > 1) ||
    5129           5 :                 (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
    5130           0 :                  tif->tif_dir.td_nstrips !=
    5131           0 :                      (uint32_t)tif->tif_dir.td_samplesperpixel))
    5132             :             {
    5133           0 :                 MissingRequired(tif, "StripByteCounts");
    5134           0 :                 goto bad;
    5135             :             }
    5136           5 :             TIFFWarningExtR(
    5137             :                 tif, module,
    5138             :                 "TIFF directory is missing required "
    5139             :                 "\"StripByteCounts\" field, calculating from imagelength");
    5140           5 :             if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    5141           0 :                 goto bad;
    5142             :         }
    5143       53973 :         else if (tif->tif_dir.td_nstrips == 1 &&
    5144       38197 :                  !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
    5145             :         {
    5146             :             /*
    5147             :              * XXX: Plexus (and others) sometimes give a value of
    5148             :              * zero for a tag when they don't know what the
    5149             :              * correct value is!  Try and handle the simple case
    5150             :              * of estimating the size of a one strip image.
    5151             :              */
    5152           5 :             TIFFWarningExtR(tif, module,
    5153             :                             "Bogus \"StripByteCounts\" field, ignoring and "
    5154             :                             "calculating from imagelength");
    5155           5 :             if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    5156           0 :                 goto bad;
    5157             :         }
    5158       53944 :         else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
    5159       19001 :                  tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    5160       14543 :                  tif->tif_dir.td_nstrips > 2 &&
    5161        3683 :                  tif->tif_dir.td_compression == COMPRESSION_NONE &&
    5162        1259 :                  TIFFGetStrileByteCount(tif, 0) !=
    5163        1259 :                      TIFFGetStrileByteCount(tif, 1) &&
    5164           0 :                  TIFFGetStrileByteCount(tif, 0) != 0 &&
    5165           0 :                  TIFFGetStrileByteCount(tif, 1) != 0)
    5166             :         {
    5167             :             /*
    5168             :              * XXX: Some vendors fill StripByteCount array with
    5169             :              * absolutely wrong values (it can be equal to
    5170             :              * StripOffset array, for example). Catch this case
    5171             :              * here.
    5172             :              *
    5173             :              * We avoid this check if deferring strile loading
    5174             :              * as it would always force us to load the strip/tile
    5175             :              * information.
    5176             :              */
    5177           0 :             TIFFWarningExtR(tif, module,
    5178             :                             "Wrong \"StripByteCounts\" field, ignoring and "
    5179             :                             "calculating from imagelength");
    5180           0 :             if (EstimateStripByteCounts(tif, dir, dircount) < 0)
    5181           0 :                 goto bad;
    5182             :         }
    5183             :     }
    5184       53945 :     if (dir)
    5185             :     {
    5186       53992 :         _TIFFfreeExt(tif, dir);
    5187       54042 :         dir = NULL;
    5188             :     }
    5189       53995 :     if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
    5190             :     {
    5191       54039 :         if (tif->tif_dir.td_bitspersample >= 16)
    5192       10886 :             tif->tif_dir.td_maxsamplevalue = 0xFFFF;
    5193             :         else
    5194       43153 :             tif->tif_dir.td_maxsamplevalue =
    5195       43153 :                 (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
    5196             :     }
    5197             : 
    5198             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    5199             :     /*
    5200             :      * XXX: We can optimize checking for the strip bounds using the sorted
    5201             :      * bytecounts array. See also comments for TIFFAppendToStrip()
    5202             :      * function in tif_write.c.
    5203             :      */
    5204             :     if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1)
    5205             :     {
    5206             :         uint32_t strip;
    5207             : 
    5208             :         tif->tif_dir.td_stripbytecountsorted = 1;
    5209             :         for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
    5210             :         {
    5211             :             if (TIFFGetStrileOffset(tif, strip - 1) >
    5212             :                 TIFFGetStrileOffset(tif, strip))
    5213             :             {
    5214             :                 tif->tif_dir.td_stripbytecountsorted = 0;
    5215             :                 break;
    5216             :             }
    5217             :         }
    5218             :     }
    5219             : #endif
    5220             : 
    5221             :     /*
    5222             :      * An opportunity for compression mode dependent tag fixup
    5223             :      */
    5224       53995 :     (*tif->tif_fixuptags)(tif);
    5225             : 
    5226             :     /*
    5227             :      * Some manufacturers make life difficult by writing
    5228             :      * large amounts of uncompressed data as a single strip.
    5229             :      * This is contrary to the recommendations of the spec.
    5230             :      * The following makes an attempt at breaking such images
    5231             :      * into strips closer to the recommended 8k bytes.  A
    5232             :      * side effect, however, is that the RowsPerStrip tag
    5233             :      * value may be changed.
    5234             :      */
    5235       54020 :     if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
    5236       44619 :         (tif->tif_dir.td_nstrips == 1) &&
    5237       36190 :         (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
    5238       29667 :         ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
    5239             :     {
    5240       21974 :         ChopUpSingleUncompressedStrip(tif);
    5241             :     }
    5242             : 
    5243             :     /* There are also uncompressed striped files with strips larger than */
    5244             :     /* 2 GB, which make them unfriendly with a lot of code. If possible, */
    5245             :     /* try to expose smaller "virtual" strips. */
    5246       54029 :     if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
    5247       44622 :         tif->tif_dir.td_compression == COMPRESSION_NONE &&
    5248       59374 :         (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
    5249       24814 :         TIFFStripSize64(tif) > 0x7FFFFFFFUL)
    5250             :     {
    5251           5 :         TryChopUpUncompressedBigTiff(tif);
    5252             :     }
    5253             : 
    5254             :     /*
    5255             :      * Clear the dirty directory flag.
    5256             :      */
    5257       54083 :     tif->tif_flags &= ~TIFF_DIRTYDIRECT;
    5258       54083 :     tif->tif_flags &= ~TIFF_DIRTYSTRIP;
    5259             : 
    5260             :     /*
    5261             :      * Reinitialize i/o since we are starting on a new directory.
    5262             :      */
    5263       54083 :     tif->tif_row = (uint32_t)-1;
    5264       54083 :     tif->tif_curstrip = (uint32_t)-1;
    5265       54083 :     tif->tif_col = (uint32_t)-1;
    5266       54083 :     tif->tif_curtile = (uint32_t)-1;
    5267       54083 :     tif->tif_tilesize = (tmsize_t)-1;
    5268             : 
    5269       54083 :     tif->tif_scanlinesize = TIFFScanlineSize(tif);
    5270       54024 :     if (!tif->tif_scanlinesize)
    5271             :     {
    5272          32 :         TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
    5273           0 :         return (0);
    5274             :     }
    5275             : 
    5276       53992 :     if (isTiled(tif))
    5277             :     {
    5278        9279 :         tif->tif_tilesize = TIFFTileSize(tif);
    5279        9278 :         if (!tif->tif_tilesize)
    5280             :         {
    5281           0 :             TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
    5282           0 :             return (0);
    5283             :         }
    5284             :     }
    5285             :     else
    5286             :     {
    5287       44713 :         if (!TIFFStripSize(tif))
    5288             :         {
    5289          31 :             TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
    5290           0 :             return (0);
    5291             :         }
    5292             :     }
    5293       54036 :     return (1);
    5294           1 : bad:
    5295           1 :     if (dir)
    5296           1 :         _TIFFfreeExt(tif, dir);
    5297           1 :     return (0);
    5298             : } /*-- TIFFReadDirectory() --*/
    5299             : 
    5300       54078 : static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
    5301             :                                         uint16_t dircount)
    5302             : {
    5303             :     static const char module[] = "TIFFReadDirectoryCheckOrder";
    5304             :     uint32_t m;
    5305             :     uint16_t n;
    5306             :     TIFFDirEntry *o;
    5307       54078 :     m = 0;
    5308      816475 :     for (n = 0, o = dir; n < dircount; n++, o++)
    5309             :     {
    5310      762409 :         if (o->tdir_tag < m)
    5311             :         {
    5312          12 :             TIFFWarningExtR(tif, module,
    5313             :                             "Invalid TIFF directory; tags are not sorted in "
    5314             :                             "ascending order");
    5315          12 :             break;
    5316             :         }
    5317      762397 :         m = o->tdir_tag + 1;
    5318             :     }
    5319       54078 : }
    5320             : 
    5321      108106 : static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
    5322             :                                                 uint16_t dircount,
    5323             :                                                 uint16_t tagid)
    5324             : {
    5325             :     TIFFDirEntry *m;
    5326             :     uint16_t n;
    5327             :     (void)tif;
    5328      598151 :     for (m = dir, n = 0; n < dircount; m++, n++)
    5329             :     {
    5330      598116 :         if (m->tdir_tag == tagid)
    5331      108071 :             return (m);
    5332             :     }
    5333          35 :     return (0);
    5334             : }
    5335             : 
    5336     1149570 : static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
    5337             :                                            uint32_t *fii)
    5338             : {
    5339             :     int32_t ma, mb, mc;
    5340     1149570 :     ma = -1;
    5341     1149570 :     mc = (int32_t)tif->tif_nfields;
    5342             :     while (1)
    5343             :     {
    5344     7322880 :         if (ma + 1 == mc)
    5345             :         {
    5346         437 :             *fii = FAILED_FII;
    5347         437 :             return;
    5348             :         }
    5349     7322440 :         mb = (ma + mc) / 2;
    5350     7322440 :         if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
    5351     1149130 :             break;
    5352     6173310 :         if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
    5353     2339750 :             ma = mb;
    5354             :         else
    5355     3833560 :             mc = mb;
    5356             :     }
    5357             :     while (1)
    5358             :     {
    5359     1149600 :         if (mb == 0)
    5360       11378 :             break;
    5361     1138220 :         if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
    5362     1137760 :             break;
    5363         463 :         mb--;
    5364             :     }
    5365     1149130 :     *fii = mb;
    5366             : }
    5367             : 
    5368             : /*
    5369             :  * Read custom directory from the arbitrary offset.
    5370             :  * The code is very similar to TIFFReadDirectory().
    5371             :  */
    5372           0 : int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff,
    5373             :                             const TIFFFieldArray *infoarray)
    5374             : {
    5375             :     static const char module[] = "TIFFReadCustomDirectory";
    5376             :     TIFFDirEntry *dir;
    5377             :     uint16_t dircount;
    5378             :     TIFFDirEntry *dp;
    5379             :     uint16_t di;
    5380             :     const TIFFField *fip;
    5381             :     uint32_t fii;
    5382             : 
    5383           0 :     assert(infoarray != NULL);
    5384           0 :     dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
    5385           0 :     if (!dircount)
    5386             :     {
    5387           0 :         TIFFErrorExtR(tif, module,
    5388             :                       "Failed to read custom directory at offset %" PRIu64,
    5389             :                       diroff);
    5390           0 :         return 0;
    5391             :     }
    5392           0 :     TIFFReadDirectoryCheckOrder(tif, dir, dircount);
    5393             : 
    5394             :     /*
    5395             :      * Mark duplicates of any tag to be ignored (bugzilla 1994)
    5396             :      * to avoid certain pathological problems.
    5397             :      */
    5398             :     {
    5399             :         TIFFDirEntry *ma;
    5400             :         uint16_t mb;
    5401           0 :         for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
    5402             :         {
    5403             :             TIFFDirEntry *na;
    5404             :             uint16_t nb;
    5405           0 :             for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
    5406             :             {
    5407           0 :                 if (ma->tdir_tag == na->tdir_tag)
    5408             :                 {
    5409           0 :                     na->tdir_ignore = TRUE;
    5410             :                 }
    5411             :             }
    5412             :         }
    5413             :     }
    5414             : 
    5415             :     /* Free any old stuff and reinit. */
    5416           0 :     TIFFFreeDirectory(tif);
    5417             :     /* Even if custom directories do not need the default settings of a standard
    5418             :      * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e.
    5419             :      * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to
    5420             :      * be initialized, which is done in TIFFDefaultDirectory().
    5421             :      * After that, the field array for the custom tags needs to be setup again.
    5422             :      */
    5423           0 :     TIFFDefaultDirectory(tif);
    5424           0 :     _TIFFSetupFields(tif, infoarray);
    5425             : 
    5426             :     /* Allocate arrays for offset values outside IFD entry for IFD data size
    5427             :      * checking. Note: Counter are reset within TIFFFreeDirectory(). */
    5428           0 :     tif->tif_dir.td_dirdatasize_offsets =
    5429           0 :         (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
    5430           0 :             tif, dircount * sizeof(TIFFEntryOffsetAndLength));
    5431           0 :     if (tif->tif_dir.td_dirdatasize_offsets == NULL)
    5432             :     {
    5433           0 :         TIFFErrorExtR(
    5434             :             tif, module,
    5435             :             "Failed to allocate memory for counting IFD data size at reading");
    5436           0 :         if (dir)
    5437           0 :             _TIFFfreeExt(tif, dir);
    5438           0 :         return 0;
    5439             :     }
    5440             : 
    5441           0 :     for (di = 0, dp = dir; di < dircount; di++, dp++)
    5442             :     {
    5443           0 :         TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    5444           0 :         if (fii == FAILED_FII)
    5445             :         {
    5446           0 :             if (tif->tif_warn_about_unknown_tags)
    5447             :             {
    5448           0 :                 TIFFWarningExtR(tif, module,
    5449             :                                 "Unknown field with tag %" PRIu16 " (0x%" PRIx16
    5450             :                                 ") encountered",
    5451           0 :                                 dp->tdir_tag, dp->tdir_tag);
    5452             :             }
    5453           0 :             const TIFFField *fld = _TIFFCreateAnonField(
    5454           0 :                 tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
    5455           0 :             if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
    5456             :             {
    5457           0 :                 if (tif->tif_warn_about_unknown_tags)
    5458             :                 {
    5459           0 :                     TIFFWarningExtR(
    5460             :                         tif, module,
    5461             :                         "Registering anonymous field with tag %" PRIu16
    5462             :                         " (0x%" PRIx16 ") failed",
    5463           0 :                         dp->tdir_tag, dp->tdir_tag);
    5464             :                 }
    5465           0 :                 dp->tdir_ignore = TRUE;
    5466             :             }
    5467             :             else
    5468             :             {
    5469           0 :                 TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    5470           0 :                 assert(fii != FAILED_FII);
    5471             :             }
    5472             :         }
    5473           0 :         if (!dp->tdir_ignore)
    5474             :         {
    5475           0 :             fip = tif->tif_fields[fii];
    5476           0 :             if (fip->field_bit == FIELD_IGNORE)
    5477           0 :                 dp->tdir_ignore = TRUE;
    5478             :             else
    5479             :             {
    5480             :                 /* check data type */
    5481           0 :                 while ((fip->field_type != TIFF_ANY) &&
    5482           0 :                        (fip->field_type != dp->tdir_type))
    5483             :                 {
    5484           0 :                     fii++;
    5485           0 :                     if ((fii == tif->tif_nfields) ||
    5486           0 :                         (tif->tif_fields[fii]->field_tag !=
    5487           0 :                          (uint32_t)dp->tdir_tag))
    5488             :                     {
    5489           0 :                         fii = 0xFFFF;
    5490           0 :                         break;
    5491             :                     }
    5492           0 :                     fip = tif->tif_fields[fii];
    5493             :                 }
    5494           0 :                 if (fii == 0xFFFF)
    5495             :                 {
    5496           0 :                     TIFFWarningExtR(tif, module,
    5497             :                                     "Wrong data type %" PRIu16
    5498             :                                     " for \"%s\"; tag ignored",
    5499           0 :                                     dp->tdir_type, fip->field_name);
    5500           0 :                     dp->tdir_ignore = TRUE;
    5501             :                 }
    5502             :                 else
    5503             :                 {
    5504             :                     /* check count if known in advance */
    5505           0 :                     if ((fip->field_readcount != TIFF_VARIABLE) &&
    5506           0 :                         (fip->field_readcount != TIFF_VARIABLE2))
    5507             :                     {
    5508             :                         uint32_t expected;
    5509           0 :                         if (fip->field_readcount == TIFF_SPP)
    5510           0 :                             expected =
    5511           0 :                                 (uint32_t)tif->tif_dir.td_samplesperpixel;
    5512             :                         else
    5513           0 :                             expected = (uint32_t)fip->field_readcount;
    5514           0 :                         if (!CheckDirCount(tif, dp, expected))
    5515           0 :                             dp->tdir_ignore = TRUE;
    5516             :                     }
    5517             :                 }
    5518             :             }
    5519           0 :             if (!dp->tdir_ignore)
    5520             :             {
    5521           0 :                 switch (dp->tdir_tag)
    5522             :                 {
    5523           0 :                     case EXIFTAG_SUBJECTDISTANCE:
    5524           0 :                         if (!TIFFFieldIsAnonymous(fip))
    5525             :                         {
    5526             :                             /* should only be called on a Exif directory */
    5527             :                             /* when exifFields[] is active */
    5528           0 :                             (void)TIFFFetchSubjectDistance(tif, dp);
    5529             :                         }
    5530             :                         else
    5531             :                         {
    5532           0 :                             (void)TIFFFetchNormalTag(tif, dp, TRUE);
    5533             :                         }
    5534           0 :                         break;
    5535           0 :                     default:
    5536           0 :                         (void)TIFFFetchNormalTag(tif, dp, TRUE);
    5537           0 :                         break;
    5538             :                 }
    5539           0 :             } /*-- if (!dp->tdir_ignore) */
    5540             :         }
    5541             :     }
    5542             :     /* Evaluate final IFD data size. */
    5543           0 :     CalcFinalIFDdatasizeReading(tif, dircount);
    5544             : 
    5545             :     /* To be able to return from SubIFD or custom-IFD to main-IFD */
    5546           0 :     tif->tif_setdirectory_force_absolute = TRUE;
    5547           0 :     if (dir)
    5548           0 :         _TIFFfreeExt(tif, dir);
    5549           0 :     return 1;
    5550             : }
    5551             : 
    5552             : /*
    5553             :  * EXIF is important special case of custom IFD, so we have a special
    5554             :  * function to read it.
    5555             :  */
    5556           0 : int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
    5557             : {
    5558           0 :     return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
    5559             : }
    5560             : 
    5561             : /*
    5562             :  *--: EXIF-GPS custom directory reading as another special case of custom IFD.
    5563             :  */
    5564           0 : int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
    5565             : {
    5566           0 :     return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
    5567             : }
    5568             : 
    5569          10 : static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
    5570             :                                    uint16_t dircount)
    5571             : {
    5572             :     static const char module[] = "EstimateStripByteCounts";
    5573             : 
    5574             :     TIFFDirEntry *dp;
    5575          10 :     TIFFDirectory *td = &tif->tif_dir;
    5576             :     uint32_t strip;
    5577             : 
    5578             :     /* Do not try to load stripbytecount as we will compute it */
    5579          10 :     if (!_TIFFFillStrilesInternal(tif, 0))
    5580           0 :         return -1;
    5581             : 
    5582          10 :     const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
    5583          10 :     uint64_t filesize = 0;
    5584          10 :     if (allocsize > 100 * 1024 * 1024)
    5585             :     {
    5586             :         /* Before allocating a huge amount of memory for corrupted files, check
    5587             :          * if size of requested memory is not greater than file size. */
    5588           0 :         filesize = TIFFGetFileSize(tif);
    5589           0 :         if (allocsize > filesize)
    5590             :         {
    5591           0 :             TIFFWarningExtR(
    5592             :                 tif, module,
    5593             :                 "Requested memory size for StripByteCounts of %" PRIu64
    5594             :                 " is greater than filesize %" PRIu64 ". Memory not allocated",
    5595             :                 allocsize, filesize);
    5596           0 :             return -1;
    5597             :         }
    5598             :     }
    5599             : 
    5600          10 :     if (td->td_stripbytecount_p)
    5601           5 :         _TIFFfreeExt(tif, td->td_stripbytecount_p);
    5602          20 :     td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
    5603          10 :         tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
    5604          10 :     if (td->td_stripbytecount_p == NULL)
    5605           0 :         return -1;
    5606             : 
    5607          10 :     if (td->td_compression != COMPRESSION_NONE)
    5608             :     {
    5609             :         uint64_t space;
    5610             :         uint16_t n;
    5611           2 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    5612           2 :             space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
    5613             :         else
    5614           0 :             space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
    5615             :         /* calculate amount of space used by indirect values */
    5616          17 :         for (dp = dir, n = dircount; n > 0; n--, dp++)
    5617             :         {
    5618             :             uint32_t typewidth;
    5619             :             uint64_t datasize;
    5620          15 :             typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type);
    5621          15 :             if (typewidth == 0)
    5622             :             {
    5623           0 :                 TIFFErrorExtR(
    5624             :                     tif, module,
    5625             :                     "Cannot determine size of unknown tag type %" PRIu16,
    5626           0 :                     dp->tdir_type);
    5627           0 :                 return -1;
    5628             :             }
    5629          15 :             if (dp->tdir_count > UINT64_MAX / typewidth)
    5630           0 :                 return -1;
    5631          15 :             datasize = (uint64_t)typewidth * dp->tdir_count;
    5632          15 :             if (!(tif->tif_flags & TIFF_BIGTIFF))
    5633             :             {
    5634          15 :                 if (datasize <= 4)
    5635          11 :                     datasize = 0;
    5636             :             }
    5637             :             else
    5638             :             {
    5639           0 :                 if (datasize <= 8)
    5640           0 :                     datasize = 0;
    5641             :             }
    5642          15 :             if (space > UINT64_MAX - datasize)
    5643           0 :                 return -1;
    5644          15 :             space += datasize;
    5645             :         }
    5646           2 :         if (filesize == 0)
    5647           2 :             filesize = TIFFGetFileSize(tif);
    5648           2 :         if (filesize < space)
    5649             :             /* we should perhaps return in error ? */
    5650           1 :             space = filesize;
    5651             :         else
    5652           1 :             space = filesize - space;
    5653           2 :         if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
    5654           0 :             space /= td->td_samplesperpixel;
    5655           4 :         for (strip = 0; strip < td->td_nstrips; strip++)
    5656           2 :             td->td_stripbytecount_p[strip] = space;
    5657             :         /*
    5658             :          * This gross hack handles the case were the offset to
    5659             :          * the last strip is past the place where we think the strip
    5660             :          * should begin.  Since a strip of data must be contiguous,
    5661             :          * it's safe to assume that we've overestimated the amount
    5662             :          * of data in the strip and trim this number back accordingly.
    5663             :          */
    5664           2 :         strip--;
    5665           2 :         if (td->td_stripoffset_p[strip] >
    5666           2 :             UINT64_MAX - td->td_stripbytecount_p[strip])
    5667           0 :             return -1;
    5668           2 :         if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
    5669             :             filesize)
    5670             :         {
    5671           1 :             if (td->td_stripoffset_p[strip] >= filesize)
    5672             :             {
    5673             :                 /* Not sure what we should in that case... */
    5674           0 :                 td->td_stripbytecount_p[strip] = 0;
    5675             :             }
    5676             :             else
    5677             :             {
    5678           1 :                 td->td_stripbytecount_p[strip] =
    5679           1 :                     filesize - td->td_stripoffset_p[strip];
    5680             :             }
    5681             :         }
    5682             :     }
    5683           8 :     else if (isTiled(tif))
    5684             :     {
    5685           0 :         uint64_t bytespertile = TIFFTileSize64(tif);
    5686             : 
    5687           0 :         for (strip = 0; strip < td->td_nstrips; strip++)
    5688           0 :             td->td_stripbytecount_p[strip] = bytespertile;
    5689             :     }
    5690             :     else
    5691             :     {
    5692           8 :         uint64_t rowbytes = TIFFScanlineSize64(tif);
    5693           8 :         uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
    5694          16 :         for (strip = 0; strip < td->td_nstrips; strip++)
    5695             :         {
    5696           8 :             if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
    5697           0 :                 return -1;
    5698           8 :             td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
    5699             :         }
    5700             :     }
    5701          10 :     TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
    5702          10 :     if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
    5703           5 :         td->td_rowsperstrip = td->td_imagelength;
    5704          10 :     return 1;
    5705             : }
    5706             : 
    5707           0 : static void MissingRequired(TIFF *tif, const char *tagname)
    5708             : {
    5709             :     static const char module[] = "MissingRequired";
    5710             : 
    5711           0 :     TIFFErrorExtR(tif, module,
    5712             :                   "TIFF directory is missing required \"%s\" field", tagname);
    5713           0 : }
    5714             : 
    5715      291114 : static unsigned long hashFuncOffsetToNumber(const void *elt)
    5716             : {
    5717      291114 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber =
    5718             :         (const TIFFOffsetAndDirNumber *)elt;
    5719      291114 :     const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
    5720      291114 :                           ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
    5721      291114 :     return hash;
    5722             : }
    5723             : 
    5724       54847 : static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
    5725             : {
    5726       54847 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
    5727             :         (const TIFFOffsetAndDirNumber *)elt1;
    5728       54847 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
    5729             :         (const TIFFOffsetAndDirNumber *)elt2;
    5730       54847 :     return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
    5731             : }
    5732             : 
    5733      248852 : static unsigned long hashFuncNumberToOffset(const void *elt)
    5734             : {
    5735      248852 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber =
    5736             :         (const TIFFOffsetAndDirNumber *)elt;
    5737      248852 :     return offsetAndDirNumber->dirNumber;
    5738             : }
    5739             : 
    5740       13889 : static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
    5741             : {
    5742       13889 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
    5743             :         (const TIFFOffsetAndDirNumber *)elt1;
    5744       13889 :     const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
    5745             :         (const TIFFOffsetAndDirNumber *)elt2;
    5746       13889 :     return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
    5747             : }
    5748             : 
    5749             : /*
    5750             :  * Check the directory number and offset against the list of already seen
    5751             :  * directory numbers and offsets. This is a trick to prevent IFD looping.
    5752             :  * The one can create TIFF file with looped directory pointers. We will
    5753             :  * maintain a list of already seen directories and check every IFD offset
    5754             :  * and its IFD number against that list. However, the offset of an IFD number
    5755             :  * can change - e.g. when writing updates to file.
    5756             :  * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
    5757             :  * or an error has occurred.
    5758             :  */
    5759      118092 : int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
    5760             : {
    5761      118092 :     if (diroff == 0) /* no more directories */
    5762           0 :         return 0;
    5763             : 
    5764      118092 :     if (tif->tif_map_dir_offset_to_number == NULL)
    5765             :     {
    5766       70594 :         tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
    5767             :             hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
    5768       70630 :         if (tif->tif_map_dir_offset_to_number == NULL)
    5769             :         {
    5770           0 :             TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5771             :                           "Not enough memory");
    5772           0 :             return 1;
    5773             :         }
    5774             :     }
    5775             : 
    5776      118128 :     if (tif->tif_map_dir_number_to_offset == NULL)
    5777             :     {
    5778             :         /* No free callback for this map, as it shares the same items as
    5779             :          * tif->tif_map_dir_offset_to_number. */
    5780       70613 :         tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
    5781             :             hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
    5782       70663 :         if (tif->tif_map_dir_number_to_offset == NULL)
    5783             :         {
    5784           0 :             TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5785             :                           "Not enough memory");
    5786           0 :             return 1;
    5787             :         }
    5788             :     }
    5789             : 
    5790             :     /* Check if offset is already in the list:
    5791             :      * - yes: check, if offset is at the same IFD number - if not, it is an IFD
    5792             :      * loop
    5793             :      * -  no: add to list or update offset at that IFD number
    5794             :      */
    5795             :     TIFFOffsetAndDirNumber entry;
    5796      118178 :     entry.offset = diroff;
    5797      118178 :     entry.dirNumber = dirn;
    5798             : 
    5799             :     TIFFOffsetAndDirNumber *foundEntry =
    5800      118178 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5801             :             tif->tif_map_dir_offset_to_number, &entry);
    5802      118036 :     if (foundEntry)
    5803             :     {
    5804       39700 :         if (foundEntry->dirNumber == dirn)
    5805             :         {
    5806       39700 :             return 1;
    5807             :         }
    5808             :         else
    5809             :         {
    5810           0 :             TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5811             :                             "TIFF directory %d has IFD looping to directory %u "
    5812             :                             "at offset 0x%" PRIx64 " (%" PRIu64 ")",
    5813           0 :                             (int)dirn - 1, foundEntry->dirNumber, diroff,
    5814             :                             diroff);
    5815           0 :             return 0;
    5816             :         }
    5817             :     }
    5818             : 
    5819             :     /* Check if offset of an IFD has been changed and update offset of that IFD
    5820             :      * number. */
    5821       78336 :     foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5822             :         tif->tif_map_dir_number_to_offset, &entry);
    5823       78352 :     if (foundEntry)
    5824             :     {
    5825          82 :         if (foundEntry->offset != diroff)
    5826             :         {
    5827             :             TIFFOffsetAndDirNumber entryOld;
    5828          82 :             entryOld.offset = foundEntry->offset;
    5829          82 :             entryOld.dirNumber = dirn;
    5830             :             /* We must remove first from tif_map_dir_number_to_offset as the */
    5831             :             /* entry is owned (and thus freed) by */
    5832             :             /* tif_map_dir_offset_to_number */
    5833             :             TIFFOffsetAndDirNumber *foundEntryOld =
    5834          82 :                 (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5835             :                     tif->tif_map_dir_number_to_offset, &entryOld);
    5836          82 :             if (foundEntryOld)
    5837             :             {
    5838          82 :                 TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
    5839             :                                   foundEntryOld);
    5840             :             }
    5841          82 :             foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5842             :                 tif->tif_map_dir_offset_to_number, &entryOld);
    5843          82 :             if (foundEntryOld)
    5844             :             {
    5845          82 :                 TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
    5846             :                                   foundEntryOld);
    5847             :             }
    5848             : 
    5849          82 :             TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
    5850             :                 sizeof(TIFFOffsetAndDirNumber));
    5851          82 :             if (entryPtr == NULL)
    5852             :             {
    5853           0 :                 return 0;
    5854             :             }
    5855             : 
    5856             :             /* Add IFD offset and dirn to IFD directory list */
    5857          82 :             *entryPtr = entry;
    5858             : 
    5859          82 :             if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
    5860             :             {
    5861           0 :                 TIFFErrorExtR(
    5862             :                     tif, "_TIFFCheckDirNumberAndOffset",
    5863             :                     "Insertion in tif_map_dir_offset_to_number failed");
    5864           0 :                 return 0;
    5865             :             }
    5866          82 :             if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
    5867             :             {
    5868           0 :                 TIFFErrorExtR(
    5869             :                     tif, "_TIFFCheckDirNumberAndOffset",
    5870             :                     "Insertion in tif_map_dir_number_to_offset failed");
    5871           0 :                 return 0;
    5872             :             }
    5873             :         }
    5874          82 :         return 1;
    5875             :     }
    5876             : 
    5877             :     /* Arbitrary (hopefully big enough) limit */
    5878       78270 :     if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
    5879             :         TIFF_MAX_DIR_COUNT)
    5880             :     {
    5881          37 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5882             :                       "Cannot handle more than %u TIFF directories",
    5883             :                       (unsigned)TIFF_MAX_DIR_COUNT);
    5884           0 :         return 0;
    5885             :     }
    5886             : 
    5887             :     TIFFOffsetAndDirNumber *entryPtr =
    5888       78249 :         (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
    5889       78249 :     if (entryPtr == NULL)
    5890             :     {
    5891           0 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5892             :                       "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
    5893           0 :         return 0;
    5894             :     }
    5895             : 
    5896             :     /* Add IFD offset and dirn to IFD directory list */
    5897       78249 :     *entryPtr = entry;
    5898             : 
    5899       78249 :     if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
    5900             :     {
    5901           0 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5902             :                       "Insertion in tif_map_dir_offset_to_number failed");
    5903           0 :         return 0;
    5904             :     }
    5905       78249 :     if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
    5906             :     {
    5907          14 :         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
    5908             :                       "Insertion in tif_map_dir_number_to_offset failed");
    5909           0 :         return 0;
    5910             :     }
    5911             : 
    5912       78265 :     return 1;
    5913             : } /* --- _TIFFCheckDirNumberAndOffset() ---*/
    5914             : 
    5915             : /*
    5916             :  * Retrieve the matching IFD directory number of a given IFD offset
    5917             :  * from the list of directories already seen.
    5918             :  * Returns 1 if the offset was in the list and the directory number
    5919             :  * can be returned.
    5920             :  * Otherwise returns 0 or if an error occurred.
    5921             :  */
    5922       11522 : int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
    5923             : {
    5924       11522 :     if (diroff == 0) /* no more directories */
    5925           0 :         return 0;
    5926             : 
    5927             :     /* Check if offset is already in the list and return matching directory
    5928             :      * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
    5929             :      * search again in IFD list.
    5930             :      */
    5931       11522 :     if (tif->tif_map_dir_offset_to_number == NULL)
    5932           5 :         return 0;
    5933             :     TIFFOffsetAndDirNumber entry;
    5934       11517 :     entry.offset = diroff;
    5935       11517 :     entry.dirNumber = 0; /* not used */
    5936             : 
    5937             :     TIFFOffsetAndDirNumber *foundEntry =
    5938       11517 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5939             :             tif->tif_map_dir_offset_to_number, &entry);
    5940       11519 :     if (foundEntry)
    5941             :     {
    5942        9698 :         *dirn = foundEntry->dirNumber;
    5943        9698 :         return 1;
    5944             :     }
    5945             : 
    5946             :     /* This updates the directory list for all main-IFDs in the file. */
    5947        1821 :     TIFFNumberOfDirectories(tif);
    5948             : 
    5949        1821 :     foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5950             :         tif->tif_map_dir_offset_to_number, &entry);
    5951        1821 :     if (foundEntry)
    5952             :     {
    5953        1767 :         *dirn = foundEntry->dirNumber;
    5954        1767 :         return 1;
    5955             :     }
    5956             : 
    5957          54 :     return 0;
    5958             : } /*--- _TIFFGetDirNumberFromOffset() ---*/
    5959             : 
    5960             : /*
    5961             :  * Retrieve the matching IFD directory offset of a given IFD number
    5962             :  * from the list of directories already seen.
    5963             :  * Returns 1 if the offset was in the list of already seen IFDs and the
    5964             :  * directory offset can be returned. The directory list is not updated.
    5965             :  * Otherwise returns 0 or if an error occurred.
    5966             :  */
    5967       10815 : int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
    5968             : {
    5969             : 
    5970       10815 :     if (tif->tif_map_dir_number_to_offset == NULL)
    5971           0 :         return 0;
    5972             :     TIFFOffsetAndDirNumber entry;
    5973       10815 :     entry.offset = 0; /* not used */
    5974       10815 :     entry.dirNumber = dirn;
    5975             : 
    5976             :     TIFFOffsetAndDirNumber *foundEntry =
    5977       10815 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    5978             :             tif->tif_map_dir_number_to_offset, &entry);
    5979       10815 :     if (foundEntry)
    5980             :     {
    5981       10815 :         *diroff = foundEntry->offset;
    5982       10815 :         return 1;
    5983             :     }
    5984             : 
    5985           0 :     return 0;
    5986             : } /*--- _TIFFGetOffsetFromDirNumber() ---*/
    5987             : 
    5988             : /*
    5989             :  * Remove an entry from the directory list of already seen directories
    5990             :  * by directory offset.
    5991             :  * If an entry is to be removed from the list, it is also okay if the entry
    5992             :  * is not in the list or the list does not exist.
    5993             :  */
    5994        1425 : int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
    5995             : {
    5996        1425 :     if (tif->tif_map_dir_offset_to_number == NULL)
    5997           0 :         return 1;
    5998             : 
    5999             :     TIFFOffsetAndDirNumber entryOld;
    6000        1425 :     entryOld.offset = diroff;
    6001        1425 :     entryOld.dirNumber = 0;
    6002             :     /* We must remove first from tif_map_dir_number_to_offset as the
    6003             :      * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
    6004             :      * However, we need firstly to find the directory number from offset. */
    6005             : 
    6006             :     TIFFOffsetAndDirNumber *foundEntryOldOff =
    6007        1425 :         (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    6008             :             tif->tif_map_dir_offset_to_number, &entryOld);
    6009        1425 :     if (foundEntryOldOff)
    6010             :     {
    6011        1414 :         entryOld.dirNumber = foundEntryOldOff->dirNumber;
    6012        1414 :         if (tif->tif_map_dir_number_to_offset != NULL)
    6013             :         {
    6014             :             TIFFOffsetAndDirNumber *foundEntryOldDir =
    6015        1414 :                 (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
    6016             :                     tif->tif_map_dir_number_to_offset, &entryOld);
    6017        1414 :             if (foundEntryOldDir)
    6018             :             {
    6019        1414 :                 TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
    6020             :                                   foundEntryOldDir);
    6021        1414 :                 TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
    6022             :                                   foundEntryOldOff);
    6023        1414 :                 return 1;
    6024             :             }
    6025             :         }
    6026             :         else
    6027             :         {
    6028           0 :             TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
    6029             :                           "Unexpectedly tif_map_dir_number_to_offset is "
    6030             :                           "missing but tif_map_dir_offset_to_number exists.");
    6031           0 :             return 0;
    6032             :         }
    6033             :     }
    6034          11 :     return 1;
    6035             : } /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
    6036             : 
    6037             : /*
    6038             :  * Check the count field of a directory entry against a known value.  The
    6039             :  * caller is expected to skip/ignore the tag if there is a mismatch.
    6040             :  */
    6041           0 : static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
    6042             : {
    6043           0 :     if ((uint64_t)count > dir->tdir_count)
    6044             :     {
    6045           0 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    6046           0 :         TIFFWarningExtR(tif, tif->tif_name,
    6047             :                         "incorrect count for field \"%s\" (%" PRIu64
    6048             :                         ", expecting %" PRIu32 "); tag ignored",
    6049             :                         fip ? fip->field_name : "unknown tagname",
    6050             :                         dir->tdir_count, count);
    6051           0 :         return (0);
    6052             :     }
    6053           0 :     else if ((uint64_t)count < dir->tdir_count)
    6054             :     {
    6055           0 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    6056           0 :         TIFFWarningExtR(tif, tif->tif_name,
    6057             :                         "incorrect count for field \"%s\" (%" PRIu64
    6058             :                         ", expecting %" PRIu32 "); tag trimmed",
    6059             :                         fip ? fip->field_name : "unknown tagname",
    6060             :                         dir->tdir_count, count);
    6061           0 :         dir->tdir_count = count;
    6062           0 :         return (1);
    6063             :     }
    6064           0 :     return (1);
    6065             : }
    6066             : 
    6067             : /*
    6068             :  * Read IFD structure from the specified offset. If the pointer to
    6069             :  * nextdiroff variable has been specified, read it too. Function returns a
    6070             :  * number of fields in the directory or 0 if failed.
    6071             :  */
    6072       54106 : static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
    6073             :                                    TIFFDirEntry **pdir, uint64_t *nextdiroff)
    6074             : {
    6075             :     static const char module[] = "TIFFFetchDirectory";
    6076             : 
    6077             :     void *origdir;
    6078             :     uint16_t dircount16;
    6079             :     uint32_t dirsize;
    6080             :     TIFFDirEntry *dir;
    6081             :     uint8_t *ma;
    6082             :     TIFFDirEntry *mb;
    6083             :     uint16_t n;
    6084             : 
    6085       54106 :     assert(pdir);
    6086             : 
    6087       54106 :     tif->tif_diroff = diroff;
    6088       54106 :     if (nextdiroff)
    6089       54035 :         *nextdiroff = 0;
    6090       54106 :     if (!isMapped(tif))
    6091             :     {
    6092       54022 :         if (!SeekOK(tif, tif->tif_diroff))
    6093             :         {
    6094          20 :             TIFFErrorExtR(tif, module,
    6095             :                           "%s: Seek error accessing TIFF directory",
    6096             :                           tif->tif_name);
    6097           0 :             return 0;
    6098             :         }
    6099       54060 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    6100             :         {
    6101       53214 :             if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
    6102             :             {
    6103          12 :                 TIFFErrorExtR(tif, module,
    6104             :                               "%s: Can not read TIFF directory count",
    6105             :                               tif->tif_name);
    6106          12 :                 return 0;
    6107             :             }
    6108       53225 :             if (tif->tif_flags & TIFF_SWAB)
    6109         561 :                 TIFFSwabShort(&dircount16);
    6110       53189 :             if (dircount16 > 4096)
    6111             :             {
    6112           0 :                 TIFFErrorExtR(tif, module,
    6113             :                               "Sanity check on directory count failed, this is "
    6114             :                               "probably not a valid IFD offset");
    6115           0 :                 return 0;
    6116             :             }
    6117       53189 :             dirsize = 12;
    6118             :         }
    6119             :         else
    6120             :         {
    6121             :             uint64_t dircount64;
    6122         846 :             if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
    6123             :             {
    6124           0 :                 TIFFErrorExtR(tif, module,
    6125             :                               "%s: Can not read TIFF directory count",
    6126             :                               tif->tif_name);
    6127           0 :                 return 0;
    6128             :             }
    6129         846 :             if (tif->tif_flags & TIFF_SWAB)
    6130          19 :                 TIFFSwabLong8(&dircount64);
    6131         846 :             if (dircount64 > 4096)
    6132             :             {
    6133           0 :                 TIFFErrorExtR(tif, module,
    6134             :                               "Sanity check on directory count failed, this is "
    6135             :                               "probably not a valid IFD offset");
    6136           0 :                 return 0;
    6137             :             }
    6138         846 :             dircount16 = (uint16_t)dircount64;
    6139         846 :             dirsize = 20;
    6140             :         }
    6141       54035 :         origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
    6142             :                                    "to read TIFF directory");
    6143       54077 :         if (origdir == NULL)
    6144           4 :             return 0;
    6145       54073 :         if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
    6146             :         {
    6147           1 :             TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
    6148             :                           tif->tif_name);
    6149           1 :             _TIFFfreeExt(tif, origdir);
    6150           1 :             return 0;
    6151             :         }
    6152             :         /*
    6153             :          * Read offset to next directory for sequential scans if
    6154             :          * needed.
    6155             :          */
    6156       54057 :         if (nextdiroff)
    6157             :         {
    6158       54058 :             if (!(tif->tif_flags & TIFF_BIGTIFF))
    6159             :             {
    6160             :                 uint32_t nextdiroff32;
    6161       53213 :                 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
    6162           4 :                     nextdiroff32 = 0;
    6163       53230 :                 if (tif->tif_flags & TIFF_SWAB)
    6164         561 :                     TIFFSwabLong(&nextdiroff32);
    6165       53200 :                 *nextdiroff = nextdiroff32;
    6166             :             }
    6167             :             else
    6168             :             {
    6169         845 :                 if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
    6170           0 :                     *nextdiroff = 0;
    6171         846 :                 if (tif->tif_flags & TIFF_SWAB)
    6172          19 :                     TIFFSwabLong8(nextdiroff);
    6173             :             }
    6174             :         }
    6175             :     }
    6176             :     else
    6177             :     {
    6178             :         tmsize_t m;
    6179             :         tmsize_t off;
    6180          84 :         if (tif->tif_diroff > (uint64_t)INT64_MAX)
    6181             :         {
    6182           0 :             TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
    6183           0 :             return (0);
    6184             :         }
    6185          84 :         off = (tmsize_t)tif->tif_diroff;
    6186             : 
    6187             :         /*
    6188             :          * Check for integer overflow when validating the dir_off,
    6189             :          * otherwise a very high offset may cause an OOB read and
    6190             :          * crash the client. Make two comparisons instead of
    6191             :          *
    6192             :          *  off + sizeof(uint16_t) > tif->tif_size
    6193             :          *
    6194             :          * to avoid overflow.
    6195             :          */
    6196          84 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    6197             :         {
    6198           8 :             m = off + sizeof(uint16_t);
    6199           8 :             if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) ||
    6200           8 :                 (m > tif->tif_size))
    6201             :             {
    6202           0 :                 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
    6203           0 :                 return 0;
    6204             :             }
    6205             :             else
    6206             :             {
    6207           8 :                 _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
    6208             :             }
    6209           8 :             off += sizeof(uint16_t);
    6210           8 :             if (tif->tif_flags & TIFF_SWAB)
    6211           0 :                 TIFFSwabShort(&dircount16);
    6212           8 :             if (dircount16 > 4096)
    6213             :             {
    6214           0 :                 TIFFErrorExtR(tif, module,
    6215             :                               "Sanity check on directory count failed, this is "
    6216             :                               "probably not a valid IFD offset");
    6217           0 :                 return 0;
    6218             :             }
    6219           8 :             dirsize = 12;
    6220             :         }
    6221             :         else
    6222             :         {
    6223             :             uint64_t dircount64;
    6224          76 :             m = off + sizeof(uint64_t);
    6225          76 :             if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
    6226           0 :                 (m > tif->tif_size))
    6227             :             {
    6228          76 :                 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
    6229           0 :                 return 0;
    6230             :             }
    6231             :             else
    6232             :             {
    6233           0 :                 _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
    6234             :             }
    6235           0 :             off += sizeof(uint64_t);
    6236           0 :             if (tif->tif_flags & TIFF_SWAB)
    6237           0 :                 TIFFSwabLong8(&dircount64);
    6238           0 :             if (dircount64 > 4096)
    6239             :             {
    6240           0 :                 TIFFErrorExtR(tif, module,
    6241             :                               "Sanity check on directory count failed, this is "
    6242             :                               "probably not a valid IFD offset");
    6243           0 :                 return 0;
    6244             :             }
    6245           0 :             dircount16 = (uint16_t)dircount64;
    6246           0 :             dirsize = 20;
    6247             :         }
    6248           8 :         if (dircount16 == 0)
    6249             :         {
    6250           0 :             TIFFErrorExtR(tif, module,
    6251             :                           "Sanity check on directory count failed, zero tag "
    6252             :                           "directories not supported");
    6253           0 :             return 0;
    6254             :         }
    6255             :         /* Before allocating a huge amount of memory for corrupted files, check
    6256             :          * if size of requested memory is not greater than file size. */
    6257           8 :         uint64_t filesize = TIFFGetFileSize(tif);
    6258           8 :         uint64_t allocsize = (uint64_t)dircount16 * dirsize;
    6259           8 :         if (allocsize > filesize)
    6260             :         {
    6261           0 :             TIFFWarningExtR(
    6262             :                 tif, module,
    6263             :                 "Requested memory size for TIFF directory of %" PRIu64
    6264             :                 " is greater than filesize %" PRIu64
    6265             :                 ". Memory not allocated, TIFF directory not read",
    6266             :                 allocsize, filesize);
    6267           0 :             return 0;
    6268             :         }
    6269           8 :         origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
    6270             :                                    "to read TIFF directory");
    6271           8 :         if (origdir == NULL)
    6272           0 :             return 0;
    6273           8 :         m = off + dircount16 * dirsize;
    6274           8 :         if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
    6275           8 :             (m > tif->tif_size))
    6276             :         {
    6277           0 :             TIFFErrorExtR(tif, module, "Can not read TIFF directory");
    6278           0 :             _TIFFfreeExt(tif, origdir);
    6279           0 :             return 0;
    6280             :         }
    6281             :         else
    6282             :         {
    6283           8 :             _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
    6284             :         }
    6285           8 :         if (nextdiroff)
    6286             :         {
    6287           8 :             off += dircount16 * dirsize;
    6288           8 :             if (!(tif->tif_flags & TIFF_BIGTIFF))
    6289             :             {
    6290             :                 uint32_t nextdiroff32;
    6291           8 :                 m = off + sizeof(uint32_t);
    6292           8 :                 if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) ||
    6293           8 :                     (m > tif->tif_size))
    6294           0 :                     nextdiroff32 = 0;
    6295             :                 else
    6296           8 :                     _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
    6297             :                                 sizeof(uint32_t));
    6298           8 :                 if (tif->tif_flags & TIFF_SWAB)
    6299           0 :                     TIFFSwabLong(&nextdiroff32);
    6300           8 :                 *nextdiroff = nextdiroff32;
    6301             :             }
    6302             :             else
    6303             :             {
    6304           0 :                 m = off + sizeof(uint64_t);
    6305           0 :                 if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
    6306           0 :                     (m > tif->tif_size))
    6307           0 :                     *nextdiroff = 0;
    6308             :                 else
    6309           0 :                     _TIFFmemcpy(nextdiroff, tif->tif_base + off,
    6310             :                                 sizeof(uint64_t));
    6311           0 :                 if (tif->tif_flags & TIFF_SWAB)
    6312           0 :                     TIFFSwabLong8(nextdiroff);
    6313             :             }
    6314             :         }
    6315             :     }
    6316             :     /* No check against filesize needed here because "dir" should have same size
    6317             :      * than "origdir" checked above. */
    6318       54053 :     dir = (TIFFDirEntry *)_TIFFCheckMalloc(
    6319             :         tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
    6320       54075 :     if (dir == 0)
    6321             :     {
    6322           9 :         _TIFFfreeExt(tif, origdir);
    6323           0 :         return 0;
    6324             :     }
    6325       54066 :     ma = (uint8_t *)origdir;
    6326       54066 :     mb = dir;
    6327      817844 :     for (n = 0; n < dircount16; n++)
    6328             :     {
    6329      763779 :         mb->tdir_ignore = FALSE;
    6330      763779 :         if (tif->tif_flags & TIFF_SWAB)
    6331        7746 :             TIFFSwabShort((uint16_t *)ma);
    6332      763779 :         mb->tdir_tag = *(uint16_t *)ma;
    6333      763779 :         ma += sizeof(uint16_t);
    6334      763779 :         if (tif->tif_flags & TIFF_SWAB)
    6335        7746 :             TIFFSwabShort((uint16_t *)ma);
    6336      763740 :         mb->tdir_type = *(uint16_t *)ma;
    6337      763740 :         ma += sizeof(uint16_t);
    6338      763740 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    6339             :         {
    6340      751493 :             if (tif->tif_flags & TIFF_SWAB)
    6341        7537 :                 TIFFSwabLong((uint32_t *)ma);
    6342      751527 :             mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
    6343      751527 :             ma += sizeof(uint32_t);
    6344      751527 :             mb->tdir_offset.toff_long8 = 0;
    6345      751527 :             *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
    6346      751527 :             ma += sizeof(uint32_t);
    6347             :         }
    6348             :         else
    6349             :         {
    6350       12247 :             if (tif->tif_flags & TIFF_SWAB)
    6351         209 :                 TIFFSwabLong8((uint64_t *)ma);
    6352       12247 :             mb->tdir_count = TIFFReadUInt64(ma);
    6353       12247 :             ma += sizeof(uint64_t);
    6354       12247 :             mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
    6355       12251 :             ma += sizeof(uint64_t);
    6356             :         }
    6357      763778 :         mb++;
    6358             :     }
    6359       54065 :     _TIFFfreeExt(tif, origdir);
    6360       54017 :     *pdir = dir;
    6361       54017 :     return dircount16;
    6362             : }
    6363             : 
    6364             : /*
    6365             :  * Fetch a tag that is not handled by special case code.
    6366             :  */
    6367      493771 : static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
    6368             : {
    6369             :     static const char module[] = "TIFFFetchNormalTag";
    6370             :     enum TIFFReadDirEntryErr err;
    6371             :     uint32_t fii;
    6372      493771 :     const TIFFField *fip = NULL;
    6373      493771 :     TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
    6374      493819 :     if (fii == FAILED_FII)
    6375             :     {
    6376         171 :         TIFFErrorExtR(tif, "TIFFFetchNormalTag",
    6377         171 :                       "No definition found for tag %" PRIu16, dp->tdir_tag);
    6378           0 :         return 0;
    6379             :     }
    6380      493648 :     fip = tif->tif_fields[fii];
    6381      493648 :     assert(fip != NULL); /* should not happen */
    6382      493648 :     assert(fip->set_get_field_type !=
    6383             :            TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
    6384             :                                   this in specialized code */
    6385      493648 :     assert(fip->set_get_field_type !=
    6386             :            TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
    6387             :                                 the case for pseudo-tags */
    6388      493648 :     err = TIFFReadDirEntryErrOk;
    6389      493648 :     switch (fip->set_get_field_type)
    6390             :     {
    6391         386 :         case TIFF_SETGET_UNDEFINED:
    6392         386 :             TIFFErrorExtR(
    6393             :                 tif, "TIFFFetchNormalTag",
    6394             :                 "Defined set_get_field_type of custom tag %u (%s) is "
    6395             :                 "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
    6396             :                 fip->field_tag, fip->field_name);
    6397         386 :             break;
    6398       38292 :         case TIFF_SETGET_ASCII:
    6399             :         {
    6400             :             uint8_t *data;
    6401       38292 :             assert(fip->field_passcount == 0);
    6402       38292 :             err = TIFFReadDirEntryByteArray(tif, dp, &data);
    6403       38267 :             if (err == TIFFReadDirEntryErrOk)
    6404             :             {
    6405       38263 :                 size_t mb = 0;
    6406             :                 int n;
    6407       38263 :                 if (data != NULL)
    6408             :                 {
    6409       38245 :                     if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
    6410             :                     {
    6411             :                         /* optimization: if data is known to be 0 terminated, we
    6412             :                          * can use strlen() */
    6413       38288 :                         mb = strlen((const char *)data);
    6414             :                     }
    6415             :                     else
    6416             :                     {
    6417             :                         /* general case. equivalent to non-portable */
    6418             :                         /* mb = strnlen((const char*)data,
    6419             :                          * (uint32_t)dp->tdir_count); */
    6420           0 :                         uint8_t *ma = data;
    6421           0 :                         while (mb < (uint32_t)dp->tdir_count)
    6422             :                         {
    6423          27 :                             if (*ma == 0)
    6424           0 :                                 break;
    6425          27 :                             ma++;
    6426          27 :                             mb++;
    6427             :                         }
    6428             :                     }
    6429             :                 }
    6430       38263 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6431             :                 {
    6432          29 :                     if (data != NULL)
    6433           0 :                         _TIFFfreeExt(tif, data);
    6434           0 :                     return (0);
    6435             :                 }
    6436       38272 :                 if (mb + 1 < (uint32_t)dp->tdir_count)
    6437           0 :                     TIFFWarningExtR(
    6438             :                         tif, module,
    6439             :                         "ASCII value for tag \"%s\" contains null byte in "
    6440             :                         "value; value incorrectly truncated during reading due "
    6441             :                         "to implementation limitations",
    6442             :                         fip->field_name);
    6443       38272 :                 else if (mb + 1 > (uint32_t)dp->tdir_count)
    6444             :                 {
    6445           1 :                     TIFFWarningExtR(tif, module,
    6446             :                                     "ASCII value for tag \"%s\" does not end "
    6447             :                                     "in null byte. Forcing it to be null",
    6448             :                                     fip->field_name);
    6449             :                     /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
    6450             :                      * larger than MAX_SIZE_TAG_DATA */
    6451           1 :                     assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
    6452           1 :                     uint8_t *o = (uint8_t *)_TIFFmallocExt(
    6453           1 :                         tif, (uint32_t)dp->tdir_count + 1);
    6454           1 :                     if (o == NULL)
    6455             :                     {
    6456           0 :                         if (data != NULL)
    6457           0 :                             _TIFFfreeExt(tif, data);
    6458           0 :                         return (0);
    6459             :                     }
    6460           1 :                     if (dp->tdir_count > 0)
    6461             :                     {
    6462           1 :                         _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
    6463             :                     }
    6464           1 :                     o[(uint32_t)dp->tdir_count] = 0;
    6465           1 :                     if (data != 0)
    6466           1 :                         _TIFFfreeExt(tif, data);
    6467           1 :                     data = o;
    6468             :                 }
    6469       38272 :                 n = TIFFSetField(tif, dp->tdir_tag, data);
    6470       38310 :                 if (data != 0)
    6471       38295 :                     _TIFFfreeExt(tif, data);
    6472       38306 :                 if (!n)
    6473           0 :                     return (0);
    6474             :             }
    6475             :         }
    6476       38310 :         break;
    6477           0 :         case TIFF_SETGET_UINT8:
    6478             :         {
    6479           0 :             uint8_t data = 0;
    6480           0 :             assert(fip->field_readcount == 1);
    6481           0 :             assert(fip->field_passcount == 0);
    6482           0 :             err = TIFFReadDirEntryByte(tif, dp, &data);
    6483           0 :             if (err == TIFFReadDirEntryErrOk)
    6484             :             {
    6485           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6486           0 :                     return (0);
    6487             :             }
    6488             :         }
    6489           0 :         break;
    6490           0 :         case TIFF_SETGET_SINT8:
    6491             :         {
    6492           0 :             int8_t data = 0;
    6493           0 :             assert(fip->field_readcount == 1);
    6494           0 :             assert(fip->field_passcount == 0);
    6495           0 :             err = TIFFReadDirEntrySbyte(tif, dp, &data);
    6496           0 :             if (err == TIFFReadDirEntryErrOk)
    6497             :             {
    6498           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6499           0 :                     return (0);
    6500             :             }
    6501             :         }
    6502           0 :         break;
    6503      167838 :         case TIFF_SETGET_UINT16:
    6504             :         {
    6505             :             uint16_t data;
    6506      167838 :             assert(fip->field_readcount == 1);
    6507      167838 :             assert(fip->field_passcount == 0);
    6508      167838 :             err = TIFFReadDirEntryShort(tif, dp, &data);
    6509      167860 :             if (err == TIFFReadDirEntryErrOk)
    6510             :             {
    6511      167874 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6512           0 :                     return (0);
    6513             :             }
    6514             :         }
    6515      167857 :         break;
    6516           0 :         case TIFF_SETGET_SINT16:
    6517             :         {
    6518             :             int16_t data;
    6519           0 :             assert(fip->field_readcount == 1);
    6520           0 :             assert(fip->field_passcount == 0);
    6521           0 :             err = TIFFReadDirEntrySshort(tif, dp, &data);
    6522           0 :             if (err == TIFFReadDirEntryErrOk)
    6523             :             {
    6524           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6525           0 :                     return (0);
    6526             :             }
    6527             :         }
    6528           0 :         break;
    6529      177141 :         case TIFF_SETGET_UINT32:
    6530             :         {
    6531             :             uint32_t data;
    6532      177141 :             assert(fip->field_readcount == 1);
    6533      177141 :             assert(fip->field_passcount == 0);
    6534      177141 :             err = TIFFReadDirEntryLong(tif, dp, &data);
    6535      177061 :             if (err == TIFFReadDirEntryErrOk)
    6536             :             {
    6537      177070 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6538           0 :                     return (0);
    6539             :             }
    6540             :         }
    6541      177077 :         break;
    6542           0 :         case TIFF_SETGET_SINT32:
    6543             :         {
    6544             :             int32_t data;
    6545           0 :             assert(fip->field_readcount == 1);
    6546           0 :             assert(fip->field_passcount == 0);
    6547           0 :             err = TIFFReadDirEntrySlong(tif, dp, &data);
    6548           0 :             if (err == TIFFReadDirEntryErrOk)
    6549             :             {
    6550           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6551           0 :                     return (0);
    6552             :             }
    6553             :         }
    6554           0 :         break;
    6555           8 :         case TIFF_SETGET_UINT64:
    6556             :         {
    6557             :             uint64_t data;
    6558           8 :             assert(fip->field_readcount == 1);
    6559           8 :             assert(fip->field_passcount == 0);
    6560           8 :             err = TIFFReadDirEntryLong8(tif, dp, &data);
    6561           8 :             if (err == TIFFReadDirEntryErrOk)
    6562             :             {
    6563           8 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6564           0 :                     return 0;
    6565           8 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6566           0 :                     return (0);
    6567             :             }
    6568             :         }
    6569           8 :         break;
    6570           0 :         case TIFF_SETGET_SINT64:
    6571             :         {
    6572             :             int64_t data;
    6573           0 :             assert(fip->field_readcount == 1);
    6574           0 :             assert(fip->field_passcount == 0);
    6575           0 :             err = TIFFReadDirEntrySlong8(tif, dp, &data);
    6576           0 :             if (err == TIFFReadDirEntryErrOk)
    6577             :             {
    6578           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6579           0 :                     return 0;
    6580           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6581           0 :                     return (0);
    6582             :             }
    6583             :         }
    6584           0 :         break;
    6585         248 :         case TIFF_SETGET_FLOAT:
    6586             :         {
    6587             :             float data;
    6588         248 :             assert(fip->field_readcount == 1);
    6589         248 :             assert(fip->field_passcount == 0);
    6590         248 :             err = TIFFReadDirEntryFloat(tif, dp, &data);
    6591         248 :             if (err == TIFFReadDirEntryErrOk)
    6592             :             {
    6593         248 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6594           0 :                     return 0;
    6595         248 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6596           0 :                     return (0);
    6597             :             }
    6598             :         }
    6599         248 :         break;
    6600           0 :         case TIFF_SETGET_DOUBLE:
    6601             :         {
    6602             :             double data;
    6603           0 :             assert(fip->field_readcount == 1);
    6604           0 :             assert(fip->field_passcount == 0);
    6605           0 :             err = TIFFReadDirEntryDouble(tif, dp, &data);
    6606           0 :             if (err == TIFFReadDirEntryErrOk)
    6607             :             {
    6608           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6609           0 :                     return 0;
    6610           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6611           0 :                     return (0);
    6612             :             }
    6613             :         }
    6614           0 :         break;
    6615           0 :         case TIFF_SETGET_IFD8:
    6616             :         {
    6617             :             uint64_t data;
    6618           0 :             assert(fip->field_readcount == 1);
    6619           0 :             assert(fip->field_passcount == 0);
    6620           0 :             err = TIFFReadDirEntryIfd8(tif, dp, &data);
    6621           0 :             if (err == TIFFReadDirEntryErrOk)
    6622             :             {
    6623           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    6624           0 :                     return 0;
    6625           0 :                 if (!TIFFSetField(tif, dp->tdir_tag, data))
    6626           0 :                     return (0);
    6627             :             }
    6628             :         }
    6629           0 :         break;
    6630        1339 :         case TIFF_SETGET_UINT16_PAIR:
    6631             :         {
    6632             :             uint16_t *data;
    6633        1339 :             assert(fip->field_readcount == 2);
    6634        1339 :             assert(fip->field_passcount == 0);
    6635        1339 :             if (dp->tdir_count != 2)
    6636             :             {
    6637           0 :                 TIFFWarningExtR(tif, module,
    6638             :                                 "incorrect count for field \"%s\", expected 2, "
    6639             :                                 "got %" PRIu64,
    6640             :                                 fip->field_name, dp->tdir_count);
    6641           0 :                 return (0);
    6642             :             }
    6643        1339 :             err = TIFFReadDirEntryShortArray(tif, dp, &data);
    6644        1339 :             if (err == TIFFReadDirEntryErrOk)
    6645             :             {
    6646             :                 int m;
    6647        1339 :                 assert(data); /* avoid CLang static Analyzer false positive */
    6648        1339 :                 m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
    6649        1339 :                 _TIFFfreeExt(tif, data);
    6650        1339 :                 if (!m)
    6651           0 :                     return (0);
    6652             :             }
    6653             :         }
    6654        1339 :         break;
    6655           0 :         case TIFF_SETGET_C0_UINT8:
    6656             :         {
    6657             :             uint8_t *data;
    6658           0 :             assert(fip->field_readcount >= 1);
    6659           0 :             assert(fip->field_passcount == 0);
    6660           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6661             :             {
    6662           0 :                 TIFFWarningExtR(tif, module,
    6663             :                                 "incorrect count for field \"%s\", expected "
    6664             :                                 "%d, got %" PRIu64,
    6665           0 :                                 fip->field_name, (int)fip->field_readcount,
    6666             :                                 dp->tdir_count);
    6667           0 :                 return (0);
    6668             :             }
    6669             :             else
    6670             :             {
    6671           0 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    6672           0 :                 if (err == TIFFReadDirEntryErrOk)
    6673             :                 {
    6674           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6675             :                     {
    6676           0 :                         if (data != 0)
    6677           0 :                             _TIFFfreeExt(tif, data);
    6678           0 :                         return 0;
    6679             :                     }
    6680             :                     int m;
    6681           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6682           0 :                     if (data != 0)
    6683           0 :                         _TIFFfreeExt(tif, data);
    6684           0 :                     if (!m)
    6685           0 :                         return (0);
    6686             :                 }
    6687             :             }
    6688             :         }
    6689           0 :         break;
    6690           0 :         case TIFF_SETGET_C0_SINT8:
    6691             :         {
    6692             :             int8_t *data;
    6693           0 :             assert(fip->field_readcount >= 1);
    6694           0 :             assert(fip->field_passcount == 0);
    6695           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6696             :             {
    6697           0 :                 TIFFWarningExtR(tif, module,
    6698             :                                 "incorrect count for field \"%s\", expected "
    6699             :                                 "%d, got %" PRIu64,
    6700           0 :                                 fip->field_name, (int)fip->field_readcount,
    6701             :                                 dp->tdir_count);
    6702           0 :                 return (0);
    6703             :             }
    6704             :             else
    6705             :             {
    6706           0 :                 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
    6707           0 :                 if (err == TIFFReadDirEntryErrOk)
    6708             :                 {
    6709           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6710             :                     {
    6711           0 :                         if (data != 0)
    6712           0 :                             _TIFFfreeExt(tif, data);
    6713           0 :                         return 0;
    6714             :                     }
    6715             :                     int m;
    6716           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6717           0 :                     if (data != 0)
    6718           0 :                         _TIFFfreeExt(tif, data);
    6719           0 :                     if (!m)
    6720           0 :                         return (0);
    6721             :                 }
    6722             :             }
    6723             :         }
    6724           0 :         break;
    6725           0 :         case TIFF_SETGET_C0_UINT16:
    6726             :         {
    6727             :             uint16_t *data;
    6728           0 :             assert(fip->field_readcount >= 1);
    6729           0 :             assert(fip->field_passcount == 0);
    6730           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6731             :             {
    6732           0 :                 TIFFWarningExtR(tif, module,
    6733             :                                 "incorrect count for field \"%s\", expected "
    6734             :                                 "%d, got %" PRIu64,
    6735           0 :                                 fip->field_name, (int)fip->field_readcount,
    6736             :                                 dp->tdir_count);
    6737           0 :                 return (0);
    6738             :             }
    6739             :             else
    6740             :             {
    6741           0 :                 err = TIFFReadDirEntryShortArray(tif, dp, &data);
    6742           0 :                 if (err == TIFFReadDirEntryErrOk)
    6743             :                 {
    6744           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6745             :                     {
    6746           0 :                         if (data != 0)
    6747           0 :                             _TIFFfreeExt(tif, data);
    6748           0 :                         return 0;
    6749             :                     }
    6750             :                     int m;
    6751           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6752           0 :                     if (data != 0)
    6753           0 :                         _TIFFfreeExt(tif, data);
    6754           0 :                     if (!m)
    6755           0 :                         return (0);
    6756             :                 }
    6757             :             }
    6758             :         }
    6759           0 :         break;
    6760           0 :         case TIFF_SETGET_C0_SINT16:
    6761             :         {
    6762             :             int16_t *data;
    6763           0 :             assert(fip->field_readcount >= 1);
    6764           0 :             assert(fip->field_passcount == 0);
    6765           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6766             :             {
    6767           0 :                 TIFFWarningExtR(tif, module,
    6768             :                                 "incorrect count for field \"%s\", expected "
    6769             :                                 "%d, got %" PRIu64,
    6770           0 :                                 fip->field_name, (int)fip->field_readcount,
    6771             :                                 dp->tdir_count);
    6772           0 :                 return (0);
    6773             :             }
    6774             :             else
    6775             :             {
    6776           0 :                 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
    6777           0 :                 if (err == TIFFReadDirEntryErrOk)
    6778             :                 {
    6779           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6780             :                     {
    6781           0 :                         if (data != 0)
    6782           0 :                             _TIFFfreeExt(tif, data);
    6783           0 :                         return 0;
    6784             :                     }
    6785             :                     int m;
    6786           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6787           0 :                     if (data != 0)
    6788           0 :                         _TIFFfreeExt(tif, data);
    6789           0 :                     if (!m)
    6790           0 :                         return (0);
    6791             :                 }
    6792             :             }
    6793             :         }
    6794           0 :         break;
    6795           0 :         case TIFF_SETGET_C0_UINT32:
    6796             :         {
    6797             :             uint32_t *data;
    6798           0 :             assert(fip->field_readcount >= 1);
    6799           0 :             assert(fip->field_passcount == 0);
    6800           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6801             :             {
    6802           0 :                 TIFFWarningExtR(tif, module,
    6803             :                                 "incorrect count for field \"%s\", expected "
    6804             :                                 "%d, got %" PRIu64,
    6805           0 :                                 fip->field_name, (int)fip->field_readcount,
    6806             :                                 dp->tdir_count);
    6807           0 :                 return (0);
    6808             :             }
    6809             :             else
    6810             :             {
    6811           0 :                 err = TIFFReadDirEntryLongArray(tif, dp, &data);
    6812           0 :                 if (err == TIFFReadDirEntryErrOk)
    6813             :                 {
    6814           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6815             :                     {
    6816           0 :                         if (data != 0)
    6817           0 :                             _TIFFfreeExt(tif, data);
    6818           0 :                         return 0;
    6819             :                     }
    6820             :                     int m;
    6821           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6822           0 :                     if (data != 0)
    6823           0 :                         _TIFFfreeExt(tif, data);
    6824           0 :                     if (!m)
    6825           0 :                         return (0);
    6826             :                 }
    6827             :             }
    6828             :         }
    6829           0 :         break;
    6830           0 :         case TIFF_SETGET_C0_SINT32:
    6831             :         {
    6832             :             int32_t *data;
    6833           0 :             assert(fip->field_readcount >= 1);
    6834           0 :             assert(fip->field_passcount == 0);
    6835           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6836             :             {
    6837           0 :                 TIFFWarningExtR(tif, module,
    6838             :                                 "incorrect count for field \"%s\", expected "
    6839             :                                 "%d, got %" PRIu64,
    6840           0 :                                 fip->field_name, (int)fip->field_readcount,
    6841             :                                 dp->tdir_count);
    6842           0 :                 return (0);
    6843             :             }
    6844             :             else
    6845             :             {
    6846           0 :                 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
    6847           0 :                 if (err == TIFFReadDirEntryErrOk)
    6848             :                 {
    6849           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6850             :                     {
    6851           0 :                         if (data != 0)
    6852           0 :                             _TIFFfreeExt(tif, data);
    6853           0 :                         return 0;
    6854             :                     }
    6855             :                     int m;
    6856           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6857           0 :                     if (data != 0)
    6858           0 :                         _TIFFfreeExt(tif, data);
    6859           0 :                     if (!m)
    6860           0 :                         return (0);
    6861             :                 }
    6862             :             }
    6863             :         }
    6864           0 :         break;
    6865           0 :         case TIFF_SETGET_C0_UINT64:
    6866             :         {
    6867             :             uint64_t *data;
    6868           0 :             assert(fip->field_readcount >= 1);
    6869           0 :             assert(fip->field_passcount == 0);
    6870           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6871             :             {
    6872           0 :                 TIFFWarningExtR(tif, module,
    6873             :                                 "incorrect count for field \"%s\", expected "
    6874             :                                 "%d, got %" PRIu64,
    6875           0 :                                 fip->field_name, (int)fip->field_readcount,
    6876             :                                 dp->tdir_count);
    6877           0 :                 return (0);
    6878             :             }
    6879             :             else
    6880             :             {
    6881           0 :                 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
    6882           0 :                 if (err == TIFFReadDirEntryErrOk)
    6883             :                 {
    6884           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6885             :                     {
    6886           0 :                         if (data != 0)
    6887           0 :                             _TIFFfreeExt(tif, data);
    6888           0 :                         return 0;
    6889             :                     }
    6890             :                     int m;
    6891           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6892           0 :                     if (data != 0)
    6893           0 :                         _TIFFfreeExt(tif, data);
    6894           0 :                     if (!m)
    6895           0 :                         return (0);
    6896             :                 }
    6897             :             }
    6898             :         }
    6899           0 :         break;
    6900           0 :         case TIFF_SETGET_C0_SINT64:
    6901             :         {
    6902             :             int64_t *data;
    6903           0 :             assert(fip->field_readcount >= 1);
    6904           0 :             assert(fip->field_passcount == 0);
    6905           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6906             :             {
    6907           0 :                 TIFFWarningExtR(tif, module,
    6908             :                                 "incorrect count for field \"%s\", expected "
    6909             :                                 "%d, got %" PRIu64,
    6910           0 :                                 fip->field_name, (int)fip->field_readcount,
    6911             :                                 dp->tdir_count);
    6912           0 :                 return (0);
    6913             :             }
    6914             :             else
    6915             :             {
    6916           0 :                 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
    6917           0 :                 if (err == TIFFReadDirEntryErrOk)
    6918             :                 {
    6919           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6920             :                     {
    6921           0 :                         if (data != 0)
    6922           0 :                             _TIFFfreeExt(tif, data);
    6923           0 :                         return 0;
    6924             :                     }
    6925             :                     int m;
    6926           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6927           0 :                     if (data != 0)
    6928           0 :                         _TIFFfreeExt(tif, data);
    6929           0 :                     if (!m)
    6930           0 :                         return (0);
    6931             :                 }
    6932             :             }
    6933             :         }
    6934           0 :         break;
    6935        1027 :         case TIFF_SETGET_C0_FLOAT:
    6936             :         {
    6937             :             float *data;
    6938        1027 :             assert(fip->field_readcount >= 1);
    6939        1027 :             assert(fip->field_passcount == 0);
    6940        1027 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6941             :             {
    6942           0 :                 TIFFWarningExtR(tif, module,
    6943             :                                 "incorrect count for field \"%s\", expected "
    6944             :                                 "%d, got %" PRIu64,
    6945           0 :                                 fip->field_name, (int)fip->field_readcount,
    6946             :                                 dp->tdir_count);
    6947           0 :                 return (0);
    6948             :             }
    6949             :             else
    6950             :             {
    6951        1027 :                 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
    6952        1027 :                 if (err == TIFFReadDirEntryErrOk)
    6953             :                 {
    6954        1027 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6955             :                     {
    6956           0 :                         if (data != 0)
    6957           0 :                             _TIFFfreeExt(tif, data);
    6958           0 :                         return 0;
    6959             :                     }
    6960             :                     int m;
    6961        1027 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6962        1027 :                     if (data != 0)
    6963        1027 :                         _TIFFfreeExt(tif, data);
    6964        1027 :                     if (!m)
    6965           0 :                         return (0);
    6966             :                 }
    6967             :             }
    6968             :         }
    6969        1027 :         break;
    6970             :         /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
    6971             :          * into Double-Arrays. */
    6972           0 :         case TIFF_SETGET_C0_DOUBLE:
    6973             :         {
    6974             :             double *data;
    6975           0 :             assert(fip->field_readcount >= 1);
    6976           0 :             assert(fip->field_passcount == 0);
    6977           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    6978             :             {
    6979           0 :                 TIFFWarningExtR(tif, module,
    6980             :                                 "incorrect count for field \"%s\", expected "
    6981             :                                 "%d, got %" PRIu64,
    6982           0 :                                 fip->field_name, (int)fip->field_readcount,
    6983             :                                 dp->tdir_count);
    6984           0 :                 return (0);
    6985             :             }
    6986             :             else
    6987             :             {
    6988           0 :                 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    6989           0 :                 if (err == TIFFReadDirEntryErrOk)
    6990             :                 {
    6991           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    6992             :                     {
    6993           0 :                         if (data != 0)
    6994           0 :                             _TIFFfreeExt(tif, data);
    6995           0 :                         return 0;
    6996             :                     }
    6997             :                     int m;
    6998           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    6999           0 :                     if (data != 0)
    7000           0 :                         _TIFFfreeExt(tif, data);
    7001           0 :                     if (!m)
    7002           0 :                         return (0);
    7003             :                 }
    7004             :             }
    7005             :         }
    7006           0 :         break;
    7007           0 :         case TIFF_SETGET_C0_IFD8:
    7008             :         {
    7009             :             uint64_t *data;
    7010           0 :             assert(fip->field_readcount >= 1);
    7011           0 :             assert(fip->field_passcount == 0);
    7012           0 :             if (dp->tdir_count != (uint64_t)fip->field_readcount)
    7013             :             {
    7014           0 :                 TIFFWarningExtR(tif, module,
    7015             :                                 "incorrect count for field \"%s\", expected "
    7016             :                                 "%d, got %" PRIu64,
    7017           0 :                                 fip->field_name, (int)fip->field_readcount,
    7018             :                                 dp->tdir_count);
    7019           0 :                 return (0);
    7020             :             }
    7021             :             else
    7022             :             {
    7023           0 :                 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
    7024           0 :                 if (err == TIFFReadDirEntryErrOk)
    7025             :                 {
    7026           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7027             :                     {
    7028           0 :                         if (data != 0)
    7029           0 :                             _TIFFfreeExt(tif, data);
    7030           0 :                         return 0;
    7031             :                     }
    7032             :                     int m;
    7033           0 :                     m = TIFFSetField(tif, dp->tdir_tag, data);
    7034           0 :                     if (data != 0)
    7035           0 :                         _TIFFfreeExt(tif, data);
    7036           0 :                     if (!m)
    7037           0 :                         return (0);
    7038             :                 }
    7039             :             }
    7040             :         }
    7041           0 :         break;
    7042           0 :         case TIFF_SETGET_C16_ASCII:
    7043             :         {
    7044             :             uint8_t *data;
    7045           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7046           0 :             assert(fip->field_passcount == 1);
    7047           0 :             if (dp->tdir_count > 0xFFFF)
    7048           0 :                 err = TIFFReadDirEntryErrCount;
    7049             :             else
    7050             :             {
    7051           0 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7052           0 :                 if (err == TIFFReadDirEntryErrOk)
    7053             :                 {
    7054           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7055             :                     {
    7056           0 :                         if (data != 0)
    7057           0 :                             _TIFFfreeExt(tif, data);
    7058           0 :                         return 0;
    7059             :                     }
    7060             :                     int m;
    7061           0 :                     if (data != 0 && dp->tdir_count > 0 &&
    7062           0 :                         data[dp->tdir_count - 1] != '\0')
    7063             :                     {
    7064           0 :                         TIFFWarningExtR(tif, module,
    7065             :                                         "ASCII value for ASCII array tag "
    7066             :                                         "\"%s\" does not end in null "
    7067             :                                         "byte. Forcing it to be null",
    7068             :                                         fip->field_name);
    7069             :                         /* Enlarge buffer and add terminating null. */
    7070           0 :                         uint8_t *o = (uint8_t *)_TIFFmallocExt(
    7071           0 :                             tif, (uint32_t)dp->tdir_count + 1);
    7072           0 :                         if (o == NULL)
    7073             :                         {
    7074           0 :                             if (data != NULL)
    7075           0 :                                 _TIFFfreeExt(tif, data);
    7076           0 :                             return (0);
    7077             :                         }
    7078           0 :                         if (dp->tdir_count > 0)
    7079             :                         {
    7080           0 :                             _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
    7081             :                         }
    7082           0 :                         o[(uint32_t)dp->tdir_count] = 0;
    7083           0 :                         dp->tdir_count++; /* Increment for added null. */
    7084           0 :                         if (data != 0)
    7085           0 :                             _TIFFfreeExt(tif, data);
    7086           0 :                         data = o;
    7087             :                     }
    7088           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7089           0 :                                      (uint16_t)(dp->tdir_count), data);
    7090           0 :                     if (data != 0)
    7091           0 :                         _TIFFfreeExt(tif, data);
    7092           0 :                     if (!m)
    7093           0 :                         return (0);
    7094             :                 }
    7095             :             }
    7096             :         }
    7097           0 :         break;
    7098           0 :         case TIFF_SETGET_C16_UINT8:
    7099             :         {
    7100             :             uint8_t *data;
    7101           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7102           0 :             assert(fip->field_passcount == 1);
    7103           0 :             if (dp->tdir_count > 0xFFFF)
    7104           0 :                 err = TIFFReadDirEntryErrCount;
    7105             :             else
    7106             :             {
    7107           0 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7108           0 :                 if (err == TIFFReadDirEntryErrOk)
    7109             :                 {
    7110           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7111             :                     {
    7112           0 :                         if (data != 0)
    7113           0 :                             _TIFFfreeExt(tif, data);
    7114           0 :                         return 0;
    7115             :                     }
    7116             :                     int m;
    7117           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7118           0 :                                      (uint16_t)(dp->tdir_count), data);
    7119           0 :                     if (data != 0)
    7120           0 :                         _TIFFfreeExt(tif, data);
    7121           0 :                     if (!m)
    7122           0 :                         return (0);
    7123             :                 }
    7124             :             }
    7125             :         }
    7126           0 :         break;
    7127           0 :         case TIFF_SETGET_C16_SINT8:
    7128             :         {
    7129             :             int8_t *data;
    7130           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7131           0 :             assert(fip->field_passcount == 1);
    7132           0 :             if (dp->tdir_count > 0xFFFF)
    7133           0 :                 err = TIFFReadDirEntryErrCount;
    7134             :             else
    7135             :             {
    7136           0 :                 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
    7137           0 :                 if (err == TIFFReadDirEntryErrOk)
    7138             :                 {
    7139           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7140             :                     {
    7141           0 :                         if (data != 0)
    7142           0 :                             _TIFFfreeExt(tif, data);
    7143           0 :                         return 0;
    7144             :                     }
    7145             :                     int m;
    7146           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7147           0 :                                      (uint16_t)(dp->tdir_count), data);
    7148           0 :                     if (data != 0)
    7149           0 :                         _TIFFfreeExt(tif, data);
    7150           0 :                     if (!m)
    7151           0 :                         return (0);
    7152             :                 }
    7153             :             }
    7154             :         }
    7155           0 :         break;
    7156       33890 :         case TIFF_SETGET_C16_UINT16:
    7157             :         {
    7158             :             uint16_t *data;
    7159       33890 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7160       33890 :             assert(fip->field_passcount == 1);
    7161       33890 :             if (dp->tdir_count > 0xFFFF)
    7162           0 :                 err = TIFFReadDirEntryErrCount;
    7163             :             else
    7164             :             {
    7165       33890 :                 err = TIFFReadDirEntryShortArray(tif, dp, &data);
    7166       33848 :                 if (err == TIFFReadDirEntryErrOk)
    7167             :                 {
    7168       33865 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7169             :                     {
    7170           0 :                         if (data != 0)
    7171           0 :                             _TIFFfreeExt(tif, data);
    7172           0 :                         return 0;
    7173             :                     }
    7174             :                     int m;
    7175       33877 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7176       33877 :                                      (uint16_t)(dp->tdir_count), data);
    7177       33915 :                     if (data != 0)
    7178       33925 :                         _TIFFfreeExt(tif, data);
    7179       33923 :                     if (!m)
    7180           0 :                         return (0);
    7181             :                 }
    7182             :             }
    7183             :         }
    7184       33906 :         break;
    7185           0 :         case TIFF_SETGET_C16_SINT16:
    7186             :         {
    7187             :             int16_t *data;
    7188           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7189           0 :             assert(fip->field_passcount == 1);
    7190           0 :             if (dp->tdir_count > 0xFFFF)
    7191           0 :                 err = TIFFReadDirEntryErrCount;
    7192             :             else
    7193             :             {
    7194           0 :                 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
    7195           0 :                 if (err == TIFFReadDirEntryErrOk)
    7196             :                 {
    7197           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7198             :                     {
    7199           0 :                         if (data != 0)
    7200           0 :                             _TIFFfreeExt(tif, data);
    7201           0 :                         return 0;
    7202             :                     }
    7203             :                     int m;
    7204           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7205           0 :                                      (uint16_t)(dp->tdir_count), data);
    7206           0 :                     if (data != 0)
    7207           0 :                         _TIFFfreeExt(tif, data);
    7208           0 :                     if (!m)
    7209           0 :                         return (0);
    7210             :                 }
    7211             :             }
    7212             :         }
    7213           0 :         break;
    7214           0 :         case TIFF_SETGET_C16_UINT32:
    7215             :         {
    7216             :             uint32_t *data;
    7217           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7218           0 :             assert(fip->field_passcount == 1);
    7219           0 :             if (dp->tdir_count > 0xFFFF)
    7220           0 :                 err = TIFFReadDirEntryErrCount;
    7221             :             else
    7222             :             {
    7223           0 :                 err = TIFFReadDirEntryLongArray(tif, dp, &data);
    7224           0 :                 if (err == TIFFReadDirEntryErrOk)
    7225             :                 {
    7226           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7227             :                     {
    7228           0 :                         if (data != 0)
    7229           0 :                             _TIFFfreeExt(tif, data);
    7230           0 :                         return 0;
    7231             :                     }
    7232             :                     int m;
    7233           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7234           0 :                                      (uint16_t)(dp->tdir_count), data);
    7235           0 :                     if (data != 0)
    7236           0 :                         _TIFFfreeExt(tif, data);
    7237           0 :                     if (!m)
    7238           0 :                         return (0);
    7239             :                 }
    7240             :             }
    7241             :         }
    7242           0 :         break;
    7243           0 :         case TIFF_SETGET_C16_SINT32:
    7244             :         {
    7245             :             int32_t *data;
    7246           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7247           0 :             assert(fip->field_passcount == 1);
    7248           0 :             if (dp->tdir_count > 0xFFFF)
    7249           0 :                 err = TIFFReadDirEntryErrCount;
    7250             :             else
    7251             :             {
    7252           0 :                 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
    7253           0 :                 if (err == TIFFReadDirEntryErrOk)
    7254             :                 {
    7255           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7256             :                     {
    7257           0 :                         if (data != 0)
    7258           0 :                             _TIFFfreeExt(tif, data);
    7259           0 :                         return 0;
    7260             :                     }
    7261             :                     int m;
    7262           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7263           0 :                                      (uint16_t)(dp->tdir_count), data);
    7264           0 :                     if (data != 0)
    7265           0 :                         _TIFFfreeExt(tif, data);
    7266           0 :                     if (!m)
    7267           0 :                         return (0);
    7268             :                 }
    7269             :             }
    7270             :         }
    7271           0 :         break;
    7272           0 :         case TIFF_SETGET_C16_UINT64:
    7273             :         {
    7274             :             uint64_t *data;
    7275           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7276           0 :             assert(fip->field_passcount == 1);
    7277           0 :             if (dp->tdir_count > 0xFFFF)
    7278           0 :                 err = TIFFReadDirEntryErrCount;
    7279             :             else
    7280             :             {
    7281           0 :                 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
    7282           0 :                 if (err == TIFFReadDirEntryErrOk)
    7283             :                 {
    7284           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7285             :                     {
    7286           0 :                         if (data != 0)
    7287           0 :                             _TIFFfreeExt(tif, data);
    7288           0 :                         return 0;
    7289             :                     }
    7290             :                     int m;
    7291           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7292           0 :                                      (uint16_t)(dp->tdir_count), data);
    7293           0 :                     if (data != 0)
    7294           0 :                         _TIFFfreeExt(tif, data);
    7295           0 :                     if (!m)
    7296           0 :                         return (0);
    7297             :                 }
    7298             :             }
    7299             :         }
    7300           0 :         break;
    7301           0 :         case TIFF_SETGET_C16_SINT64:
    7302             :         {
    7303             :             int64_t *data;
    7304           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7305           0 :             assert(fip->field_passcount == 1);
    7306           0 :             if (dp->tdir_count > 0xFFFF)
    7307           0 :                 err = TIFFReadDirEntryErrCount;
    7308             :             else
    7309             :             {
    7310           0 :                 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
    7311           0 :                 if (err == TIFFReadDirEntryErrOk)
    7312             :                 {
    7313           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7314             :                     {
    7315           0 :                         if (data != 0)
    7316           0 :                             _TIFFfreeExt(tif, data);
    7317           0 :                         return 0;
    7318             :                     }
    7319             :                     int m;
    7320           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7321           0 :                                      (uint16_t)(dp->tdir_count), data);
    7322           0 :                     if (data != 0)
    7323           0 :                         _TIFFfreeExt(tif, data);
    7324           0 :                     if (!m)
    7325           0 :                         return (0);
    7326             :                 }
    7327             :             }
    7328             :         }
    7329           0 :         break;
    7330           0 :         case TIFF_SETGET_C16_FLOAT:
    7331             :         {
    7332             :             float *data;
    7333           0 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7334           0 :             assert(fip->field_passcount == 1);
    7335           0 :             if (dp->tdir_count > 0xFFFF)
    7336           0 :                 err = TIFFReadDirEntryErrCount;
    7337             :             else
    7338             :             {
    7339           0 :                 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
    7340           0 :                 if (err == TIFFReadDirEntryErrOk)
    7341             :                 {
    7342           0 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7343             :                     {
    7344           0 :                         if (data != 0)
    7345           0 :                             _TIFFfreeExt(tif, data);
    7346           0 :                         return 0;
    7347             :                     }
    7348             :                     int m;
    7349           0 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7350           0 :                                      (uint16_t)(dp->tdir_count), data);
    7351           0 :                     if (data != 0)
    7352           0 :                         _TIFFfreeExt(tif, data);
    7353           0 :                     if (!m)
    7354           0 :                         return (0);
    7355             :                 }
    7356             :             }
    7357             :         }
    7358           0 :         break;
    7359       70464 :         case TIFF_SETGET_C16_DOUBLE:
    7360             :         {
    7361             :             double *data;
    7362       70464 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7363       70464 :             assert(fip->field_passcount == 1);
    7364       70464 :             if (dp->tdir_count > 0xFFFF)
    7365           0 :                 err = TIFFReadDirEntryErrCount;
    7366             :             else
    7367             :             {
    7368       70464 :                 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    7369       70425 :                 if (err == TIFFReadDirEntryErrOk)
    7370             :                 {
    7371       70447 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7372             :                     {
    7373           0 :                         if (data != 0)
    7374           0 :                             _TIFFfreeExt(tif, data);
    7375           0 :                         return 0;
    7376             :                     }
    7377             :                     int m;
    7378       70442 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7379       70442 :                                      (uint16_t)(dp->tdir_count), data);
    7380       70455 :                     if (data != 0)
    7381       70442 :                         _TIFFfreeExt(tif, data);
    7382       70504 :                     if (!m)
    7383           0 :                         return (0);
    7384             :                 }
    7385             :             }
    7386             :         }
    7387       70482 :         break;
    7388          81 :         case TIFF_SETGET_C16_IFD8:
    7389             :         {
    7390             :             uint64_t *data;
    7391          81 :             assert(fip->field_readcount == TIFF_VARIABLE);
    7392          81 :             assert(fip->field_passcount == 1);
    7393          81 :             if (dp->tdir_count > 0xFFFF)
    7394           0 :                 err = TIFFReadDirEntryErrCount;
    7395             :             else
    7396             :             {
    7397          81 :                 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
    7398          81 :                 if (err == TIFFReadDirEntryErrOk)
    7399             :                 {
    7400          81 :                     if (!EvaluateIFDdatasizeReading(tif, dp))
    7401             :                     {
    7402           0 :                         if (data != 0)
    7403           0 :                             _TIFFfreeExt(tif, data);
    7404           0 :                         return 0;
    7405             :                     }
    7406             :                     int m;
    7407          81 :                     m = TIFFSetField(tif, dp->tdir_tag,
    7408          81 :                                      (uint16_t)(dp->tdir_count), data);
    7409          81 :                     if (data != 0)
    7410          81 :                         _TIFFfreeExt(tif, data);
    7411          81 :                     if (!m)
    7412           0 :                         return (0);
    7413             :                 }
    7414             :             }
    7415             :         }
    7416          81 :         break;
    7417           9 :         case TIFF_SETGET_C32_ASCII:
    7418             :         {
    7419             :             uint8_t *data;
    7420           9 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7421           9 :             assert(fip->field_passcount == 1);
    7422           9 :             err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7423           9 :             if (err == TIFFReadDirEntryErrOk)
    7424             :             {
    7425           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7426             :                 {
    7427           0 :                     if (data != 0)
    7428           0 :                         _TIFFfreeExt(tif, data);
    7429           0 :                     return 0;
    7430             :                 }
    7431             :                 int m;
    7432           0 :                 if (data != 0 && dp->tdir_count > 0 &&
    7433           0 :                     data[dp->tdir_count - 1] != '\0')
    7434             :                 {
    7435           0 :                     TIFFWarningExtR(
    7436             :                         tif, module,
    7437             :                         "ASCII value for ASCII array tag \"%s\" does not end "
    7438             :                         "in null byte. Forcing it to be null",
    7439             :                         fip->field_name);
    7440             :                     /* Enlarge buffer and add terminating null. */
    7441           0 :                     uint8_t *o = (uint8_t *)_TIFFmallocExt(
    7442           0 :                         tif, (uint32_t)dp->tdir_count + 1);
    7443           0 :                     if (o == NULL)
    7444             :                     {
    7445           0 :                         if (data != NULL)
    7446           0 :                             _TIFFfreeExt(tif, data);
    7447           0 :                         return (0);
    7448             :                     }
    7449           0 :                     if (dp->tdir_count > 0)
    7450             :                     {
    7451           0 :                         _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
    7452             :                     }
    7453           0 :                     o[(uint32_t)dp->tdir_count] = 0;
    7454           0 :                     dp->tdir_count++; /* Increment for added null. */
    7455           0 :                     if (data != 0)
    7456           0 :                         _TIFFfreeExt(tif, data);
    7457           0 :                     data = o;
    7458             :                 }
    7459           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7460             :                                  data);
    7461           0 :                 if (data != 0)
    7462           0 :                     _TIFFfreeExt(tif, data);
    7463           0 :                 if (!m)
    7464           0 :                     return (0);
    7465             :             }
    7466             :         }
    7467           9 :         break;
    7468        2089 :         case TIFF_SETGET_C32_UINT8:
    7469             :         {
    7470             :             uint8_t *data;
    7471        2089 :             uint32_t count = 0;
    7472        2089 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7473        2089 :             assert(fip->field_passcount == 1);
    7474        2089 :             if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
    7475           0 :                 dp->tdir_type == TIFF_LONG)
    7476           0 :             {
    7477             :                 /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
    7478             :                  * data type LONG instead of UNDEFINED. Work around this
    7479             :                  * frequently found issue */
    7480             :                 void *origdata;
    7481           0 :                 err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
    7482           0 :                 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
    7483             :                 {
    7484           0 :                     data = NULL;
    7485             :                 }
    7486             :                 else
    7487             :                 {
    7488           0 :                     if (tif->tif_flags & TIFF_SWAB)
    7489           0 :                         TIFFSwabArrayOfLong((uint32_t *)origdata, count);
    7490           0 :                     data = (uint8_t *)origdata;
    7491           0 :                     count = (uint32_t)(count * 4);
    7492             :                 }
    7493             :             }
    7494             :             else
    7495             :             {
    7496        2089 :                 err = TIFFReadDirEntryByteArray(tif, dp, &data);
    7497        2089 :                 count = (uint32_t)(dp->tdir_count);
    7498             :             }
    7499        2089 :             if (err == TIFFReadDirEntryErrOk)
    7500             :             {
    7501        2080 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7502             :                 {
    7503           0 :                     if (data != 0)
    7504           0 :                         _TIFFfreeExt(tif, data);
    7505           0 :                     return 0;
    7506             :                 }
    7507             :                 int m;
    7508        2080 :                 m = TIFFSetField(tif, dp->tdir_tag, count, data);
    7509        2080 :                 if (data != 0)
    7510        2080 :                     _TIFFfreeExt(tif, data);
    7511        2080 :                 if (!m)
    7512           0 :                     return (0);
    7513             :             }
    7514             :         }
    7515        2089 :         break;
    7516           0 :         case TIFF_SETGET_C32_SINT8:
    7517             :         {
    7518           0 :             int8_t *data = NULL;
    7519           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7520           0 :             assert(fip->field_passcount == 1);
    7521           0 :             err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
    7522           0 :             if (err == TIFFReadDirEntryErrOk)
    7523             :             {
    7524           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7525             :                 {
    7526           0 :                     if (data != 0)
    7527           0 :                         _TIFFfreeExt(tif, data);
    7528           0 :                     return 0;
    7529             :                 }
    7530             :                 int m;
    7531           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7532             :                                  data);
    7533           0 :                 if (data != 0)
    7534           0 :                     _TIFFfreeExt(tif, data);
    7535           0 :                 if (!m)
    7536           0 :                     return (0);
    7537             :             }
    7538             :         }
    7539           0 :         break;
    7540          12 :         case TIFF_SETGET_C32_UINT16:
    7541             :         {
    7542             :             uint16_t *data;
    7543          12 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7544          12 :             assert(fip->field_passcount == 1);
    7545          12 :             err = TIFFReadDirEntryShortArray(tif, dp, &data);
    7546          12 :             if (err == TIFFReadDirEntryErrOk)
    7547             :             {
    7548          12 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7549             :                 {
    7550           0 :                     if (data != 0)
    7551           0 :                         _TIFFfreeExt(tif, data);
    7552           0 :                     return 0;
    7553             :                 }
    7554             :                 int m;
    7555          12 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7556             :                                  data);
    7557          12 :                 if (data != 0)
    7558          12 :                     _TIFFfreeExt(tif, data);
    7559          12 :                 if (!m)
    7560           0 :                     return (0);
    7561             :             }
    7562             :         }
    7563          12 :         break;
    7564           0 :         case TIFF_SETGET_C32_SINT16:
    7565             :         {
    7566           0 :             int16_t *data = NULL;
    7567           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7568           0 :             assert(fip->field_passcount == 1);
    7569           0 :             err = TIFFReadDirEntrySshortArray(tif, dp, &data);
    7570           0 :             if (err == TIFFReadDirEntryErrOk)
    7571             :             {
    7572           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7573             :                 {
    7574           0 :                     if (data != 0)
    7575           0 :                         _TIFFfreeExt(tif, data);
    7576           0 :                     return 0;
    7577             :                 }
    7578             :                 int m;
    7579           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7580             :                                  data);
    7581           0 :                 if (data != 0)
    7582           0 :                     _TIFFfreeExt(tif, data);
    7583           0 :                 if (!m)
    7584           0 :                     return (0);
    7585             :             }
    7586             :         }
    7587           0 :         break;
    7588         801 :         case TIFF_SETGET_C32_UINT32:
    7589             :         {
    7590             :             uint32_t *data;
    7591         801 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7592         801 :             assert(fip->field_passcount == 1);
    7593         801 :             err = TIFFReadDirEntryLongArray(tif, dp, &data);
    7594         801 :             if (err == TIFFReadDirEntryErrOk)
    7595             :             {
    7596         792 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7597             :                 {
    7598           0 :                     if (data != 0)
    7599           0 :                         _TIFFfreeExt(tif, data);
    7600           0 :                     return 0;
    7601             :                 }
    7602             :                 int m;
    7603         792 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7604             :                                  data);
    7605         792 :                 if (data != 0)
    7606         792 :                     _TIFFfreeExt(tif, data);
    7607         792 :                 if (!m)
    7608           0 :                     return (0);
    7609             :             }
    7610             :         }
    7611         801 :         break;
    7612           0 :         case TIFF_SETGET_C32_SINT32:
    7613             :         {
    7614           0 :             int32_t *data = NULL;
    7615           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7616           0 :             assert(fip->field_passcount == 1);
    7617           0 :             err = TIFFReadDirEntrySlongArray(tif, dp, &data);
    7618           0 :             if (err == TIFFReadDirEntryErrOk)
    7619             :             {
    7620           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7621             :                 {
    7622           0 :                     if (data != 0)
    7623           0 :                         _TIFFfreeExt(tif, data);
    7624           0 :                     return 0;
    7625             :                 }
    7626             :                 int m;
    7627           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7628             :                                  data);
    7629           0 :                 if (data != 0)
    7630           0 :                     _TIFFfreeExt(tif, data);
    7631           0 :                 if (!m)
    7632           0 :                     return (0);
    7633             :             }
    7634             :         }
    7635           0 :         break;
    7636           6 :         case TIFF_SETGET_C32_UINT64:
    7637             :         {
    7638             :             uint64_t *data;
    7639           6 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7640           6 :             assert(fip->field_passcount == 1);
    7641           6 :             err = TIFFReadDirEntryLong8Array(tif, dp, &data);
    7642           6 :             if (err == TIFFReadDirEntryErrOk)
    7643             :             {
    7644           6 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7645             :                 {
    7646           0 :                     if (data != 0)
    7647           0 :                         _TIFFfreeExt(tif, data);
    7648           0 :                     return 0;
    7649             :                 }
    7650             :                 int m;
    7651           6 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7652             :                                  data);
    7653           6 :                 if (data != 0)
    7654           6 :                     _TIFFfreeExt(tif, data);
    7655           6 :                 if (!m)
    7656           0 :                     return (0);
    7657             :             }
    7658             :         }
    7659           6 :         break;
    7660           0 :         case TIFF_SETGET_C32_SINT64:
    7661             :         {
    7662           0 :             int64_t *data = NULL;
    7663           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7664           0 :             assert(fip->field_passcount == 1);
    7665           0 :             err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
    7666           0 :             if (err == TIFFReadDirEntryErrOk)
    7667             :             {
    7668           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7669             :                 {
    7670           0 :                     if (data != 0)
    7671           0 :                         _TIFFfreeExt(tif, data);
    7672           0 :                     return 0;
    7673             :                 }
    7674             :                 int m;
    7675           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7676             :                                  data);
    7677           0 :                 if (data != 0)
    7678           0 :                     _TIFFfreeExt(tif, data);
    7679           0 :                 if (!m)
    7680           0 :                     return (0);
    7681             :             }
    7682             :         }
    7683           0 :         break;
    7684           0 :         case TIFF_SETGET_C32_FLOAT:
    7685             :         {
    7686             :             float *data;
    7687           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7688           0 :             assert(fip->field_passcount == 1);
    7689           0 :             err = TIFFReadDirEntryFloatArray(tif, dp, &data);
    7690           0 :             if (err == TIFFReadDirEntryErrOk)
    7691             :             {
    7692           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7693             :                 {
    7694           0 :                     if (data != 0)
    7695           0 :                         _TIFFfreeExt(tif, data);
    7696           0 :                     return 0;
    7697             :                 }
    7698             :                 int m;
    7699           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7700             :                                  data);
    7701           0 :                 if (data != 0)
    7702           0 :                     _TIFFfreeExt(tif, data);
    7703           0 :                 if (!m)
    7704           0 :                     return (0);
    7705             :             }
    7706             :         }
    7707           0 :         break;
    7708           0 :         case TIFF_SETGET_C32_DOUBLE:
    7709             :         {
    7710             :             double *data;
    7711           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7712           0 :             assert(fip->field_passcount == 1);
    7713           0 :             err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
    7714           0 :             if (err == TIFFReadDirEntryErrOk)
    7715             :             {
    7716           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7717             :                 {
    7718           0 :                     if (data != 0)
    7719           0 :                         _TIFFfreeExt(tif, data);
    7720           0 :                     return 0;
    7721             :                 }
    7722             :                 int m;
    7723           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7724             :                                  data);
    7725           0 :                 if (data != 0)
    7726           0 :                     _TIFFfreeExt(tif, data);
    7727           0 :                 if (!m)
    7728           0 :                     return (0);
    7729             :             }
    7730             :         }
    7731           0 :         break;
    7732           0 :         case TIFF_SETGET_C32_IFD8:
    7733             :         {
    7734             :             uint64_t *data;
    7735           0 :             assert(fip->field_readcount == TIFF_VARIABLE2);
    7736           0 :             assert(fip->field_passcount == 1);
    7737           0 :             err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
    7738           0 :             if (err == TIFFReadDirEntryErrOk)
    7739             :             {
    7740           0 :                 if (!EvaluateIFDdatasizeReading(tif, dp))
    7741             :                 {
    7742           0 :                     if (data != 0)
    7743           0 :                         _TIFFfreeExt(tif, data);
    7744           0 :                     return 0;
    7745             :                 }
    7746             :                 int m;
    7747           0 :                 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
    7748             :                                  data);
    7749           0 :                 if (data != 0)
    7750           0 :                     _TIFFfreeExt(tif, data);
    7751           0 :                 if (!m)
    7752           0 :                     return (0);
    7753             :             }
    7754             :         }
    7755           0 :         break;
    7756           0 :         case TIFF_SETGET_INT:
    7757             :         case TIFF_SETGET_C0_ASCII:
    7758             :         case TIFF_SETGET_OTHER:
    7759           0 :             assert(0); /* these should not arrive here */
    7760             :             break;
    7761          17 :         default:
    7762          17 :             assert(0); /* we should never get here */
    7763             :             break;
    7764             :     }
    7765      493638 :     if (err != TIFFReadDirEntryErrOk)
    7766             :     {
    7767          36 :         TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
    7768          36 :         return (0);
    7769             :     }
    7770      493602 :     return (1);
    7771             : }
    7772             : 
    7773             : /*
    7774             :  * Fetch a set of offsets or lengths.
    7775             :  * While this routine says "strips", in fact it's also used for tiles.
    7776             :  */
    7777       84699 : static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
    7778             :                                uint64_t **lpp)
    7779             : {
    7780             :     static const char module[] = "TIFFFetchStripThing";
    7781             :     enum TIFFReadDirEntryErr err;
    7782             :     uint64_t *data;
    7783       84699 :     err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
    7784       84696 :     if (err != TIFFReadDirEntryErrOk)
    7785             :     {
    7786           2 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    7787           2 :         TIFFReadDirEntryOutputErr(tif, err, module,
    7788             :                                   fip ? fip->field_name : "unknown tagname", 0);
    7789           2 :         return (0);
    7790             :     }
    7791       84694 :     if (dir->tdir_count < (uint64_t)nstrips)
    7792             :     {
    7793             :         uint64_t *resizeddata;
    7794           4 :         const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
    7795           4 :         const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
    7796           4 :         uint32_t max_nstrips = 1000000;
    7797           4 :         if (pszMax)
    7798           0 :             max_nstrips = (uint32_t)atoi(pszMax);
    7799           4 :         TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
    7800             :                                   fip ? fip->field_name : "unknown tagname",
    7801             :                                   (nstrips <= max_nstrips));
    7802             : 
    7803           4 :         if (nstrips > max_nstrips)
    7804             :         {
    7805           0 :             _TIFFfreeExt(tif, data);
    7806           0 :             return (0);
    7807             :         }
    7808             : 
    7809           4 :         const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
    7810           4 :         if (allocsize > 100 * 1024 * 1024)
    7811             :         {
    7812             :             /* Before allocating a huge amount of memory for corrupted files,
    7813             :              * check if size of requested memory is not greater than file size.
    7814             :              */
    7815           0 :             const uint64_t filesize = TIFFGetFileSize(tif);
    7816           0 :             if (allocsize > filesize)
    7817             :             {
    7818           0 :                 TIFFWarningExtR(
    7819             :                     tif, module,
    7820             :                     "Requested memory size for StripArray of %" PRIu64
    7821             :                     " is greater than filesize %" PRIu64
    7822             :                     ". Memory not allocated",
    7823             :                     allocsize, filesize);
    7824           0 :                 _TIFFfreeExt(tif, data);
    7825           0 :                 return (0);
    7826             :             }
    7827             :         }
    7828           4 :         resizeddata = (uint64_t *)_TIFFCheckMalloc(
    7829             :             tif, nstrips, sizeof(uint64_t), "for strip array");
    7830           4 :         if (resizeddata == 0)
    7831             :         {
    7832           0 :             _TIFFfreeExt(tif, data);
    7833           0 :             return (0);
    7834             :         }
    7835           4 :         if (dir->tdir_count)
    7836           4 :             _TIFFmemcpy(resizeddata, data,
    7837           4 :                         (uint32_t)dir->tdir_count * sizeof(uint64_t));
    7838           4 :         _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
    7839           4 :                     (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
    7840           4 :         _TIFFfreeExt(tif, data);
    7841          24 :         data = resizeddata;
    7842             :     }
    7843       84714 :     *lpp = data;
    7844       84714 :     return (1);
    7845             : }
    7846             : 
    7847             : /*
    7848             :  * Fetch and set the SubjectDistance EXIF tag.
    7849             :  */
    7850           0 : static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
    7851             : {
    7852             :     static const char module[] = "TIFFFetchSubjectDistance";
    7853             :     enum TIFFReadDirEntryErr err;
    7854             :     UInt64Aligned_t m;
    7855           0 :     m.l = 0;
    7856             :     assert(sizeof(double) == 8);
    7857             :     assert(sizeof(uint64_t) == 8);
    7858             :     assert(sizeof(uint32_t) == 4);
    7859           0 :     if (dir->tdir_count != 1)
    7860           0 :         err = TIFFReadDirEntryErrCount;
    7861           0 :     else if (dir->tdir_type != TIFF_RATIONAL)
    7862           0 :         err = TIFFReadDirEntryErrType;
    7863             :     else
    7864             :     {
    7865           0 :         if (!(tif->tif_flags & TIFF_BIGTIFF))
    7866             :         {
    7867             :             uint32_t offset;
    7868           0 :             offset = *(uint32_t *)(&dir->tdir_offset);
    7869           0 :             if (tif->tif_flags & TIFF_SWAB)
    7870           0 :                 TIFFSwabLong(&offset);
    7871           0 :             err = TIFFReadDirEntryData(tif, offset, 8, m.i);
    7872             :         }
    7873             :         else
    7874             :         {
    7875           0 :             m.l = dir->tdir_offset.toff_long8;
    7876           0 :             err = TIFFReadDirEntryErrOk;
    7877             :         }
    7878             :     }
    7879           0 :     if (err == TIFFReadDirEntryErrOk)
    7880             :     {
    7881             :         double n;
    7882           0 :         if (tif->tif_flags & TIFF_SWAB)
    7883           0 :             TIFFSwabArrayOfLong(m.i, 2);
    7884           0 :         if (m.i[0] == 0)
    7885           0 :             n = 0.0;
    7886           0 :         else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
    7887             :             /*
    7888             :              * XXX: Numerator 0xFFFFFFFF means that we have infinite
    7889             :              * distance. Indicate that with a negative floating point
    7890             :              * SubjectDistance value.
    7891             :              */
    7892           0 :             n = -1.0;
    7893             :         else
    7894           0 :             n = (double)m.i[0] / (double)m.i[1];
    7895           0 :         return (TIFFSetField(tif, dir->tdir_tag, n));
    7896             :     }
    7897             :     else
    7898             :     {
    7899           0 :         TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
    7900           0 :         return (0);
    7901             :     }
    7902             : }
    7903             : 
    7904         159 : static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
    7905             :                                       uint64_t stripbytes,
    7906             :                                       uint32_t rowsperstrip)
    7907             : {
    7908         159 :     TIFFDirectory *td = &tif->tif_dir;
    7909             :     uint64_t bytecount;
    7910             :     uint64_t offset;
    7911             :     uint64_t last_offset;
    7912             :     uint64_t last_bytecount;
    7913             :     uint32_t i;
    7914             :     uint64_t *newcounts;
    7915             :     uint64_t *newoffsets;
    7916             : 
    7917         159 :     offset = TIFFGetStrileOffset(tif, 0);
    7918         159 :     last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
    7919         159 :     last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
    7920         159 :     if (last_offset > UINT64_MAX - last_bytecount ||
    7921         159 :         last_offset + last_bytecount < offset)
    7922             :     {
    7923           0 :         return;
    7924             :     }
    7925         159 :     bytecount = last_offset + last_bytecount - offset;
    7926             : 
    7927             :     /* Before allocating a huge amount of memory for corrupted files, check if
    7928             :      * size of StripByteCount and StripOffset tags is not greater than
    7929             :      * file size.
    7930             :      */
    7931         159 :     const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
    7932         159 :     if (allocsize > 100 * 1024 * 1024)
    7933             :     {
    7934           0 :         const uint64_t filesize = TIFFGetFileSize(tif);
    7935           0 :         if (allocsize > filesize)
    7936             :         {
    7937           0 :             TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
    7938             :                             "Requested memory size for StripByteCount and "
    7939             :                             "StripOffsets %" PRIu64
    7940             :                             " is greater than filesize %" PRIu64
    7941             :                             ". Memory not allocated",
    7942             :                             allocsize, filesize);
    7943           0 :             return;
    7944             :         }
    7945             :     }
    7946             : 
    7947             :     newcounts =
    7948         159 :         (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
    7949             :                                      "for chopped \"StripByteCounts\" array");
    7950         159 :     newoffsets = (uint64_t *)_TIFFCheckMalloc(
    7951             :         tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
    7952         159 :     if (newcounts == NULL || newoffsets == NULL)
    7953             :     {
    7954             :         /*
    7955             :          * Unable to allocate new strip information, give up and use
    7956             :          * the original one strip information.
    7957             :          */
    7958           0 :         if (newcounts != NULL)
    7959           0 :             _TIFFfreeExt(tif, newcounts);
    7960           0 :         if (newoffsets != NULL)
    7961           0 :             _TIFFfreeExt(tif, newoffsets);
    7962           0 :         return;
    7963             :     }
    7964             : 
    7965             :     /*
    7966             :      * Fill the strip information arrays with new bytecounts and offsets
    7967             :      * that reflect the broken-up format.
    7968             :      */
    7969      106924 :     for (i = 0; i < nstrips; i++)
    7970             :     {
    7971      106765 :         if (stripbytes > bytecount)
    7972          53 :             stripbytes = bytecount;
    7973      106765 :         newcounts[i] = stripbytes;
    7974      106765 :         newoffsets[i] = stripbytes ? offset : 0;
    7975      106765 :         offset += stripbytes;
    7976      106765 :         bytecount -= stripbytes;
    7977             :     }
    7978             : 
    7979             :     /*
    7980             :      * Replace old single strip info with multi-strip info.
    7981             :      */
    7982         159 :     td->td_stripsperimage = td->td_nstrips = nstrips;
    7983         159 :     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
    7984             : 
    7985         159 :     _TIFFfreeExt(tif, td->td_stripbytecount_p);
    7986         159 :     _TIFFfreeExt(tif, td->td_stripoffset_p);
    7987         159 :     td->td_stripbytecount_p = newcounts;
    7988         159 :     td->td_stripoffset_p = newoffsets;
    7989             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    7990             :     td->td_stripbytecountsorted = 1;
    7991             : #endif
    7992         159 :     tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
    7993             : }
    7994             : 
    7995             : /*
    7996             :  * Replace a single strip (tile) of uncompressed data by multiple strips
    7997             :  * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
    7998             :  * dealing with large images or for dealing with machines with a limited
    7999             :  * amount memory.
    8000             :  */
    8001       22034 : static void ChopUpSingleUncompressedStrip(TIFF *tif)
    8002             : {
    8003       22034 :     TIFFDirectory *td = &tif->tif_dir;
    8004             :     uint64_t bytecount;
    8005             :     uint64_t offset;
    8006             :     uint32_t rowblock;
    8007             :     uint64_t rowblockbytes;
    8008             :     uint64_t stripbytes;
    8009             :     uint32_t nstrips;
    8010             :     uint32_t rowsperstrip;
    8011             : 
    8012       22034 :     bytecount = TIFFGetStrileByteCount(tif, 0);
    8013             :     /* On a newly created file, just re-opened to be filled, we */
    8014             :     /* don't want strip chop to trigger as it is going to cause issues */
    8015             :     /* later ( StripOffsets and StripByteCounts improperly filled) . */
    8016       22001 :     if (bytecount == 0 && tif->tif_mode != O_RDONLY)
    8017         554 :         return;
    8018       21447 :     offset = TIFFGetStrileByteCount(tif, 0);
    8019       21468 :     assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
    8020       21448 :     if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
    8021           1 :         rowblock = td->td_ycbcrsubsampling[1];
    8022             :     else
    8023       21447 :         rowblock = 1;
    8024       21448 :     rowblockbytes = TIFFVTileSize64(tif, rowblock);
    8025             :     /*
    8026             :      * Make the rows hold at least one scanline, but fill specified amount
    8027             :      * of data if possible.
    8028             :      */
    8029       21443 :     if (rowblockbytes > STRIP_SIZE_DEFAULT)
    8030             :     {
    8031          23 :         stripbytes = rowblockbytes;
    8032          23 :         rowsperstrip = rowblock;
    8033             :     }
    8034       21420 :     else if (rowblockbytes > 0)
    8035             :     {
    8036             :         uint32_t rowblocksperstrip;
    8037       21369 :         rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
    8038       21369 :         rowsperstrip = rowblocksperstrip * rowblock;
    8039       21369 :         stripbytes = rowblocksperstrip * rowblockbytes;
    8040             :     }
    8041             :     else
    8042          51 :         return;
    8043             : 
    8044             :     /*
    8045             :      * never increase the number of rows per strip
    8046             :      */
    8047       21392 :     if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
    8048       21234 :         return;
    8049         158 :     nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
    8050         158 :     if (nstrips == 0)
    8051           0 :         return;
    8052             : 
    8053             :     /* If we are going to allocate a lot of memory, make sure that the */
    8054             :     /* file is as big as needed */
    8055         159 :     if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
    8056           1 :         (offset >= TIFFGetFileSize(tif) ||
    8057           0 :          stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
    8058             :     {
    8059           1 :         return;
    8060             :     }
    8061             : 
    8062         157 :     allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
    8063             : }
    8064             : 
    8065             : /*
    8066             :  * Replace a file with contiguous strips > 2 GB of uncompressed data by
    8067             :  * multiple smaller strips. This is useful for
    8068             :  * dealing with large images or for dealing with machines with a limited
    8069             :  * amount memory.
    8070             :  */
    8071           5 : static void TryChopUpUncompressedBigTiff(TIFF *tif)
    8072             : {
    8073           5 :     TIFFDirectory *td = &tif->tif_dir;
    8074             :     uint32_t rowblock;
    8075             :     uint64_t rowblockbytes;
    8076             :     uint32_t i;
    8077             :     uint64_t stripsize;
    8078             :     uint32_t rowblocksperstrip;
    8079             :     uint32_t rowsperstrip;
    8080             :     uint64_t stripbytes;
    8081             :     uint32_t nstrips;
    8082             : 
    8083           5 :     stripsize = TIFFStripSize64(tif);
    8084             : 
    8085           5 :     assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
    8086           5 :     assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
    8087           5 :     assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
    8088             :            TIFF_STRIPCHOP);
    8089           5 :     assert(stripsize > 0x7FFFFFFFUL);
    8090             : 
    8091             :     /* On a newly created file, just re-opened to be filled, we */
    8092             :     /* don't want strip chop to trigger as it is going to cause issues */
    8093             :     /* later ( StripOffsets and StripByteCounts improperly filled) . */
    8094           5 :     if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
    8095           0 :         return;
    8096             : 
    8097           5 :     if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
    8098           0 :         rowblock = td->td_ycbcrsubsampling[1];
    8099             :     else
    8100           5 :         rowblock = 1;
    8101           5 :     rowblockbytes = TIFFVStripSize64(tif, rowblock);
    8102           5 :     if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
    8103             :     {
    8104             :         /* In case of file with gigantic width */
    8105           3 :         return;
    8106             :     }
    8107             : 
    8108             :     /* Check that the strips are contiguous and of the expected size */
    8109           5 :     for (i = 0; i < td->td_nstrips; i++)
    8110             :     {
    8111           3 :         if (i == td->td_nstrips - 1)
    8112             :         {
    8113           4 :             if (TIFFGetStrileByteCount(tif, i) <
    8114           2 :                 TIFFVStripSize64(tif,
    8115           2 :                                  td->td_imagelength - i * td->td_rowsperstrip))
    8116             :             {
    8117           0 :                 return;
    8118             :             }
    8119             :         }
    8120             :         else
    8121             :         {
    8122           1 :             if (TIFFGetStrileByteCount(tif, i) != stripsize)
    8123             :             {
    8124           0 :                 return;
    8125             :             }
    8126           1 :             if (i > 0 && TIFFGetStrileOffset(tif, i) !=
    8127           0 :                              TIFFGetStrileOffset(tif, i - 1) +
    8128           0 :                                  TIFFGetStrileByteCount(tif, i - 1))
    8129             :             {
    8130           0 :                 return;
    8131             :             }
    8132             :         }
    8133             :     }
    8134             : 
    8135             :     /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
    8136           2 :     rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
    8137           2 :     if (rowblocksperstrip == 0)
    8138           0 :         rowblocksperstrip = 1;
    8139           2 :     rowsperstrip = rowblocksperstrip * rowblock;
    8140           2 :     stripbytes = rowblocksperstrip * rowblockbytes;
    8141           2 :     assert(stripbytes <= 0x7FFFFFFFUL);
    8142             : 
    8143           2 :     if (rowsperstrip == 0)
    8144           0 :         return;
    8145           2 :     nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
    8146           2 :     if (nstrips == 0)
    8147           0 :         return;
    8148             : 
    8149             :     /* If we are going to allocate a lot of memory, make sure that the */
    8150             :     /* file is as big as needed */
    8151           2 :     if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
    8152             :     {
    8153           0 :         uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
    8154           0 :         uint64_t filesize = TIFFGetFileSize(tif);
    8155             :         uint64_t last_bytecount =
    8156           0 :             TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
    8157           0 :         if (last_offset > filesize || last_bytecount > filesize - last_offset)
    8158             :         {
    8159           0 :             return;
    8160             :         }
    8161             :     }
    8162             : 
    8163           2 :     allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
    8164             : }
    8165             : 
    8166             : TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
    8167     6335720 : static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
    8168             : {
    8169     6335720 :     return a + b;
    8170             : }
    8171             : 
    8172             : /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
    8173             :  * strip/tile of number strile. Also fetch the neighbouring values using a
    8174             :  * 4096 byte page size.
    8175             :  */
    8176        6579 : static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent,
    8177             :                                       int strile, uint64_t *panVals)
    8178             : {
    8179             :     static const char module[] = "_TIFFPartialReadStripArray";
    8180             : #define IO_CACHE_PAGE_SIZE 4096
    8181             : 
    8182             :     size_t sizeofval;
    8183        6579 :     const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
    8184             :     int sizeofvalint;
    8185             :     uint64_t nBaseOffset;
    8186             :     uint64_t nOffset;
    8187             :     uint64_t nOffsetStartPage;
    8188             :     uint64_t nOffsetEndPage;
    8189             :     tmsize_t nToRead;
    8190             :     tmsize_t nRead;
    8191             :     uint64_t nLastStripOffset;
    8192             :     int iStartBefore;
    8193             :     int i;
    8194        6579 :     const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
    8195             :     unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
    8196             : 
    8197        6579 :     assert(dirent->tdir_count > 4);
    8198             : 
    8199        6579 :     if (dirent->tdir_type == TIFF_SHORT)
    8200             :     {
    8201        1963 :         sizeofval = sizeof(uint16_t);
    8202             :     }
    8203        4616 :     else if (dirent->tdir_type == TIFF_LONG)
    8204             :     {
    8205        4582 :         sizeofval = sizeof(uint32_t);
    8206             :     }
    8207          34 :     else if (dirent->tdir_type == TIFF_LONG8)
    8208             :     {
    8209          35 :         sizeofval = sizeof(uint64_t);
    8210             :     }
    8211           0 :     else if (dirent->tdir_type == TIFF_SLONG8)
    8212             :     {
    8213             :         /* Non conformant but used by some images as in */
    8214             :         /* https://github.com/OSGeo/gdal/issues/2165 */
    8215           0 :         sizeofval = sizeof(int64_t);
    8216             :     }
    8217             :     else
    8218             :     {
    8219           0 :         TIFFErrorExtR(tif, module,
    8220             :                       "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
    8221           0 :         panVals[strile] = 0;
    8222           0 :         return 0;
    8223             :     }
    8224        6580 :     sizeofvalint = (int)(sizeofval);
    8225             : 
    8226        6580 :     if (tif->tif_flags & TIFF_BIGTIFF)
    8227             :     {
    8228          64 :         uint64_t offset = dirent->tdir_offset.toff_long8;
    8229          64 :         if (bSwab)
    8230           0 :             TIFFSwabLong8(&offset);
    8231          64 :         nBaseOffset = offset;
    8232             :     }
    8233             :     else
    8234             :     {
    8235        6516 :         uint32_t offset = dirent->tdir_offset.toff_long;
    8236        6516 :         if (bSwab)
    8237          88 :             TIFFSwabLong(&offset);
    8238        6516 :         nBaseOffset = offset;
    8239             :     }
    8240             :     /* To avoid later unsigned integer overflows */
    8241        6580 :     if (nBaseOffset > (uint64_t)INT64_MAX)
    8242             :     {
    8243           0 :         TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
    8244             :                       strile);
    8245           0 :         panVals[strile] = 0;
    8246           0 :         return 0;
    8247             :     }
    8248        6580 :     nOffset = nBaseOffset + sizeofval * strile;
    8249        6580 :     nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
    8250        6580 :     nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
    8251             : 
    8252        6580 :     if (nOffset + sizeofval > nOffsetEndPage)
    8253        1978 :         nOffsetEndPage += IO_CACHE_PAGE_SIZE;
    8254             : #undef IO_CACHE_PAGE_SIZE
    8255             : 
    8256        6580 :     nLastStripOffset = nBaseOffset + arraySize * sizeofval;
    8257        6580 :     if (nLastStripOffset < nOffsetEndPage)
    8258        3533 :         nOffsetEndPage = nLastStripOffset;
    8259        6580 :     if (nOffsetStartPage >= nOffsetEndPage)
    8260             :     {
    8261           0 :         TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
    8262             :                       strile);
    8263           0 :         panVals[strile] = 0;
    8264           0 :         return 0;
    8265             :     }
    8266        6580 :     if (!SeekOK(tif, nOffsetStartPage))
    8267             :     {
    8268           0 :         panVals[strile] = 0;
    8269           0 :         return 0;
    8270             :     }
    8271             : 
    8272        6580 :     nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
    8273        6580 :     nRead = TIFFReadFile(tif, buffer, nToRead);
    8274        6580 :     if (nRead < nToRead)
    8275             :     {
    8276           2 :         TIFFErrorExtR(tif, module,
    8277             :                       "Cannot read offset/size for strile around ~%d", strile);
    8278           2 :         return 0;
    8279             :     }
    8280        6578 :     iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
    8281        6578 :     if (strile + iStartBefore < 0)
    8282        3563 :         iStartBefore = -strile;
    8283        6578 :     for (i = iStartBefore;
    8284    12675000 :          (uint32_t)(strile + i) < arraySize &&
    8285     6335720 :          _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
    8286             :              nOffsetEndPage;
    8287     6332660 :          ++i)
    8288             :     {
    8289     6332660 :         if (dirent->tdir_type == TIFF_SHORT)
    8290             :         {
    8291             :             uint16_t val;
    8292     2130970 :             memcpy(&val,
    8293     2130970 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8294             :                    sizeof(val));
    8295     2130970 :             if (bSwab)
    8296        5220 :                 TIFFSwabShort(&val);
    8297     2130970 :             panVals[strile + i] = val;
    8298             :         }
    8299     4201690 :         else if (dirent->tdir_type == TIFF_LONG)
    8300             :         {
    8301             :             uint32_t val;
    8302     4200230 :             memcpy(&val,
    8303     4200230 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8304             :                    sizeof(val));
    8305     4200230 :             if (bSwab)
    8306        5528 :                 TIFFSwabLong(&val);
    8307     4200210 :             panVals[strile + i] = val;
    8308             :         }
    8309        1463 :         else if (dirent->tdir_type == TIFF_LONG8)
    8310             :         {
    8311             :             uint64_t val;
    8312        1479 :             memcpy(&val,
    8313        1479 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8314             :                    sizeof(val));
    8315        1479 :             if (bSwab)
    8316           0 :                 TIFFSwabLong8(&val);
    8317        1479 :             panVals[strile + i] = val;
    8318             :         }
    8319             :         else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
    8320             :         {
    8321             :             /* Non conformant data type */
    8322             :             int64_t val;
    8323           0 :             memcpy(&val,
    8324           0 :                    buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
    8325             :                    sizeof(val));
    8326           0 :             if (bSwab)
    8327           0 :                 TIFFSwabLong8((uint64_t *)&val);
    8328           0 :             panVals[strile + i] = (uint64_t)val;
    8329             :         }
    8330             :     }
    8331        6585 :     return 1;
    8332             : }
    8333             : 
    8334     6393780 : static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
    8335             :                                  TIFFDirEntry *dirent, uint64_t **parray)
    8336             : {
    8337             :     static const char module[] = "_TIFFFetchStrileValue";
    8338     6393780 :     TIFFDirectory *td = &tif->tif_dir;
    8339     6393780 :     if (strile >= dirent->tdir_count)
    8340             :     {
    8341           0 :         return 0;
    8342             :     }
    8343     6393780 :     if (strile >= td->td_stripoffsetbyteallocsize)
    8344             :     {
    8345        1785 :         uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
    8346             :         uint32_t nStripArrayAllocNew;
    8347             :         uint64_t nArraySize64;
    8348             :         size_t nArraySize;
    8349             :         uint64_t *offsetArray;
    8350             :         uint64_t *bytecountArray;
    8351             : 
    8352        1785 :         if (strile > 1000000)
    8353             :         {
    8354           3 :             uint64_t filesize = TIFFGetFileSize(tif);
    8355             :             /* Avoid excessive memory allocation attempt */
    8356             :             /* For such a big blockid we need at least a TIFF_LONG per strile */
    8357             :             /* for the offset array. */
    8358           3 :             if (strile > filesize / sizeof(uint32_t))
    8359             :             {
    8360           2 :                 TIFFErrorExtR(tif, module, "File too short");
    8361           2 :                 return 0;
    8362             :             }
    8363             :         }
    8364             : 
    8365        1783 :         if (td->td_stripoffsetbyteallocsize == 0 &&
    8366        1782 :             td->td_nstrips < 1024 * 1024)
    8367             :         {
    8368        1781 :             nStripArrayAllocNew = td->td_nstrips;
    8369             :         }
    8370             :         else
    8371             :         {
    8372             : #define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
    8373             : #define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
    8374           2 :             nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
    8375           2 :             if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
    8376           2 :                 nStripArrayAllocNew *= 2;
    8377           2 :             nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
    8378             :         }
    8379        1783 :         assert(strile < nStripArrayAllocNew);
    8380        1783 :         nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
    8381        1783 :         nArraySize = (size_t)(nArraySize64);
    8382             : #if SIZEOF_SIZE_T == 4
    8383             :         if (nArraySize != nArraySize64)
    8384             :         {
    8385             :             TIFFErrorExtR(tif, module,
    8386             :                           "Cannot allocate strip offset and bytecount arrays");
    8387             :             return 0;
    8388             :         }
    8389             : #endif
    8390        1783 :         offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
    8391             :                                                    nArraySize));
    8392        1783 :         bytecountArray = (uint64_t *)(_TIFFreallocExt(
    8393        1783 :             tif, td->td_stripbytecount_p, nArraySize));
    8394        1783 :         if (offsetArray)
    8395        1783 :             td->td_stripoffset_p = offsetArray;
    8396        1783 :         if (bytecountArray)
    8397        1783 :             td->td_stripbytecount_p = bytecountArray;
    8398        1783 :         if (offsetArray && bytecountArray)
    8399             :         {
    8400        1783 :             td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
    8401             :             /* Initialize new entries to ~0 / -1 */
    8402             :             /* coverity[overrun-buffer-arg] */
    8403        1783 :             memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
    8404        1783 :                    (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
    8405             :                        sizeof(uint64_t));
    8406             :             /* coverity[overrun-buffer-arg] */
    8407        1783 :             memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
    8408        1783 :                    (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
    8409             :                        sizeof(uint64_t));
    8410             :         }
    8411             :         else
    8412             :         {
    8413           0 :             TIFFErrorExtR(tif, module,
    8414             :                           "Cannot allocate strip offset and bytecount arrays");
    8415           0 :             _TIFFfreeExt(tif, td->td_stripoffset_p);
    8416           0 :             td->td_stripoffset_p = NULL;
    8417           0 :             _TIFFfreeExt(tif, td->td_stripbytecount_p);
    8418           2 :             td->td_stripbytecount_p = NULL;
    8419           2 :             td->td_stripoffsetbyteallocsize = 0;
    8420             :         }
    8421             :     }
    8422     6393780 :     if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
    8423           0 :         return 0;
    8424             : 
    8425     6393780 :     if (~((*parray)[strile]) == 0)
    8426             :     {
    8427        6580 :         if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray))
    8428             :         {
    8429           2 :             (*parray)[strile] = 0;
    8430           2 :             return 0;
    8431             :         }
    8432             :     }
    8433             : 
    8434     6393780 :     return 1;
    8435             : }
    8436             : 
    8437     6897450 : static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
    8438             :                                                      TIFFDirEntry *dirent,
    8439             :                                                      uint64_t **parray,
    8440             :                                                      int *pbErr)
    8441             : {
    8442     6897450 :     TIFFDirectory *td = &tif->tif_dir;
    8443     6897450 :     if (pbErr)
    8444     4326520 :         *pbErr = 0;
    8445             : 
    8446             :     /* Check that StripOffsets and StripByteCounts tags have the same number
    8447             :      * of declared entries. Otherwise we might take the "dirent->tdir_count <=
    8448             :      * 4" code path for one of them, and the other code path for the other one,
    8449             :      * which will lead to inconsistencies and potential out-of-bounds reads.
    8450             :      */
    8451     6897450 :     if (td->td_stripoffset_entry.tdir_count !=
    8452     6897450 :         td->td_stripbytecount_entry.tdir_count)
    8453             :     {
    8454          23 :         if (pbErr)
    8455           3 :             *pbErr = 1;
    8456          23 :         return 0;
    8457             :     }
    8458             : 
    8459     6897430 :     if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
    8460     6728010 :         !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
    8461             :     {
    8462     6721430 :         if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) ||
    8463             :             /* If the values may fit in the toff_long/toff_long8 member */
    8464             :             /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
    8465     6711050 :             dirent->tdir_count <= 4)
    8466             :         {
    8467      327653 :             if (!_TIFFFillStriles(tif))
    8468             :             {
    8469           2 :                 if (pbErr)
    8470           2 :                     *pbErr = 1;
    8471             :                 /* Do not return, as we want this function to always */
    8472             :                 /* return the same value if called several times with */
    8473             :                 /* the same arguments */
    8474             :             }
    8475             :         }
    8476             :         else
    8477             :         {
    8478     6393780 :             if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
    8479             :             {
    8480           4 :                 if (pbErr)
    8481           4 :                     *pbErr = 1;
    8482           4 :                 return 0;
    8483             :             }
    8484             :         }
    8485             :     }
    8486     6897530 :     if (*parray == NULL || strile >= td->td_nstrips)
    8487             :     {
    8488          33 :         if (pbErr)
    8489           2 :             *pbErr = 1;
    8490          33 :         return 0;
    8491             :     }
    8492     6897490 :     return (*parray)[strile];
    8493             : }
    8494             : 
    8495             : /* Return the value of the TileOffsets/StripOffsets array for the specified
    8496             :  * tile/strile */
    8497     2166240 : uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
    8498             : {
    8499     2166240 :     return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
    8500             : }
    8501             : 
    8502             : /* Return the value of the TileOffsets/StripOffsets array for the specified
    8503             :  * tile/strile */
    8504     4307580 : uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
    8505             : {
    8506     4307580 :     TIFFDirectory *td = &tif->tif_dir;
    8507     4307580 :     return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
    8508             :                                                 &(td->td_stripoffset_entry),
    8509             :                                                 &(td->td_stripoffset_p), pbErr);
    8510             : }
    8511             : 
    8512             : /* Return the value of the TileByteCounts/StripByteCounts array for the
    8513             :  * specified tile/strile */
    8514      404807 : uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
    8515             : {
    8516      404807 :     return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
    8517             : }
    8518             : 
    8519             : /* Return the value of the TileByteCounts/StripByteCounts array for the
    8520             :  * specified tile/strile */
    8521     2589940 : uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
    8522             : {
    8523     2589940 :     TIFFDirectory *td = &tif->tif_dir;
    8524     2589940 :     return _TIFFGetStrileOffsetOrByteCountValue(
    8525             :         tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
    8526             :         pbErr);
    8527             : }
    8528             : 
    8529      731879 : int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
    8530             : 
    8531      731936 : static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
    8532             : {
    8533      731936 :     TIFFDirectory *td = &tif->tif_dir;
    8534      731936 :     int return_value = 1;
    8535             : 
    8536             :     /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
    8537      731936 :     if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
    8538      350129 :         (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
    8539      381865 :         return 1;
    8540             : 
    8541      350071 :     if ((tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) &&
    8542      320832 :         !(tif->tif_flags & TIFF_LAZYSTRILELOAD_DONE))
    8543             :     {
    8544             :         /* In case of lazy loading, reload completely the arrays */
    8545       23376 :         _TIFFfreeExt(tif, td->td_stripoffset_p);
    8546       23343 :         _TIFFfreeExt(tif, td->td_stripbytecount_p);
    8547       23430 :         td->td_stripoffset_p = NULL;
    8548       23430 :         td->td_stripbytecount_p = NULL;
    8549       23430 :         td->td_stripoffsetbyteallocsize = 0;
    8550       23430 :         tif->tif_flags |= TIFF_LAZYSTRILELOAD_DONE;
    8551             :     }
    8552             : 
    8553             :     /* If stripoffset array is already loaded, exit with success */
    8554      350125 :     if (td->td_stripoffset_p != NULL)
    8555      325295 :         return 1;
    8556             : 
    8557             :     /* If tdir_count was canceled, then we already got there, but in error */
    8558       24830 :     if (td->td_stripoffset_entry.tdir_count == 0)
    8559         129 :         return 0;
    8560             : 
    8561       24701 :     if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
    8562             :                              &td->td_stripoffset_p))
    8563             :     {
    8564           1 :         return_value = 0;
    8565             :     }
    8566             : 
    8567       49387 :     if (loadStripByteCount &&
    8568       24641 :         !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
    8569             :                              td->td_nstrips, &td->td_stripbytecount_p))
    8570             :     {
    8571           1 :         return_value = 0;
    8572             :     }
    8573             : 
    8574       24746 :     _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
    8575       24740 :     _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
    8576             : 
    8577             : #ifdef STRIPBYTECOUNTSORTED_UNUSED
    8578             :     if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
    8579             :     {
    8580             :         uint32_t strip;
    8581             : 
    8582             :         tif->tif_dir.td_stripbytecountsorted = 1;
    8583             :         for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
    8584             :         {
    8585             :             if (tif->tif_dir.td_stripoffset_p[strip - 1] >
    8586             :                 tif->tif_dir.td_stripoffset_p[strip])
    8587             :             {
    8588             :                 tif->tif_dir.td_stripbytecountsorted = 0;
    8589             :                 break;
    8590             :             }
    8591             :         }
    8592             :     }
    8593             : #endif
    8594             : 
    8595       24729 :     return return_value;
    8596             : }

Generated by: LCOV version 1.14