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

Generated by: LCOV version 1.14